Library Os / python

0

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

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

Could anyone 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)
    
asked by anonymous 09.12.2016 / 02:44

2 answers

1

This error is occurring because the pythontest directory does not exist.

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

if not os.path.exists(directory):
    os.makedirs(directory)
    
09.12.2016 / 14:08
-1

Just another form of condition;)

If( os.path.exists(directory) == False ): 
   os.makedirs(directory)
    
06.10.2018 / 00:33