Reputation: 47
I am processing some files from a folder that is being frequently updated. I need to add a piece of code that would check whether the file is already in a folder, and if not then go ahead with processing. If yes, then just skip and go for another one. So far I have this:
files_processed = os.listdir(path) # ['AZ_saturday_id-1', 'AZ_saturday_id-2', 'AZ_sunday_id-1', 'BY_saturday_id-1']
initials = ['AZ', 'BY', 'CX']
day = ['saturday', 'sunday']
id = [1, 2, 3, 4, 5]
files = []
for init in initials:
for d in day:
for i in id:
name = f'{init}_{d}_id-{i}'
if name in files_processed:
continue
files.append(name)
But this doesn't do what I would expect. The variable files
gets all file names created in the for loop but it should have only the ones that do not exist in the files_processed
yet.
When I try:
name in files_processed
that evaluates to True/False correctly but it doesn't work in the loop. Any ideas?
Upvotes: 0
Views: 1591
Reputation: 11
You can use the os library, like so:
import os
os.path.exists("file.txt")
Or like this:
import os.path
if os.path.isfile('filename.txt'):
print ("File exists")
else:
print ("File does not exist")
Upvotes: 1