Python does not return files inside a directory

Asked

Viewed 366 times

1

Good night I am new in Python programming but I created this code using the.listdr and did not return any results of the files inside folder. If anyone has any idea how to handle it, I’d appreciate it. follows the code:

import os

def rename_files():

#(1) Obter nomes de arquivos de uma pasta
file_list = os.listdr(r"C:\Users\Deivid\Pictures\Imagens\prank")
print (file_list)
  • In mistakes of this kind, the first thing is to use the principle KISS, try to run only with the root of the drive: file_list = os.listdr(r"C:\")

  • Thanks for the help friend, I tried to run only the root but did not succeed.

  • What was the error msg?

  • There was no error message, the code compiled but in the shell did not show the contents of the directory, only the cursor was flashing.

3 answers

1

Guys thanks for the help, but I fixed the problem and managed to run the script, below follows the reworked script:

import os
def rename_files():     
list_files = os.listdir(r"diretório")
print(list_files)
rename_files()

The problem is that it was missing to close the empty function def rename_files(): rename_files()

  • 4

    And what is the difference between this code and the question? What exactly was wrong? If this worked and did not understand why not the other, the problem was not fixed. If understood, edit the answer is describe the error.

  • Galera follows the return that was missing from the problem.

0

Apparently it’s a syntax error I ran the script here and it worked

def rename_files():
...     file_list = os.listdir(r"diretorio")
...     print(file_list)
  • I believe not friend, because the script is the same.. I am in doubt if it is something related to the Python version.

0

Friend you forgot a 'i':

import os

def rename_files():

    #(1) Obter nomes de arquivos de uma pasta
    file_list = os.listdir(r"C:\Users\Goku\MaisdeOitoMil")
    print (file_list)

rename_files()
  • Thanks for the help fixed the error here but still unsuccessful, the program runs normal but does not appear the files of the directory.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.