I can print out names of all files in my current directory using the following python script:
import psycopg2
from config import config
import os
path = 'path_to_current_dir'
params = config()
conn = None
try:
#read the connection parameters
params = config()
conn = psycopg2.connect(**params)
cur = conn.cursor()
for root, dirs, files in os.walk(path):
for file in files:
if file.endswith('.plt'):
print(file)
#f = open(file, 'r')
cur.close()
conn.commit()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
Output:
20090205094627.plt
20090312135415.plt
20090216055722.plt
20090311065333.plt
20090418032222.plt
20090119072534.plt
I want to read the content of each file, So I uncomment the open(..) statement in the script above. However, even the first file in the directory is not read, reporting the following error:
$ python check.py
20090205094627.plt
[Errno 2] No such file or directory: '20090205094627.plt'
I am missing the right logic to read these files. How do I fix this?
path.