The/ python library

Asked

Viewed 2,187 times

1

This program aims to show certain type of file you have in a folder. But it is giving the following:

erro: WindowsError: [Error 2] O sistema não pode encontrar o arquivo especificado: 'pythonteste'. 

Could someone help me solve this problem ?


import os
import glob


current_directory= os.path.dirname(os.path.abspath(__file__))
os.chdir('pythonteste')
files= glob.glob('*.txt')
for file in files:
    print(file)
  • Check if the directory pythonteste exists in the folder where you are running the project, or pass the absolute path to the folder that has the files you want. Obs: in this code current_directory is not being necessary.

  • The party was not in the project directory, now it worked ! Thank you very much

2 answers

1

This error is happening because the directory pythontest there is no.

You can create it if it does not exist by the application itself:

if not os.path.exists(directory):
    os.makedirs(directory)
  • The pythonteste folder was not in the directory that the project was running and so it was not working, Thanks for the help !

-1

Only another form of condition ;)

If( os.path.exists(directory) == False ): 
   os.makedirs(directory)

Browser other questions tagged

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