Deleting files based on day within filename
Question:
I have a directory with files like: data_Mon_15-8-22.csv
, data_Tue_16-8-22.csv
, data_Mon_22-8-22.csv
etc and I am trying to delete all but the Monday files. However, my script doesn’t seem to differentiate between the filenames and just deletes everything despite me stating it. Where did I go wrong? Any help would be much appreciated!
My Code:
def file_delete():
directory = pathlib.Path('/Path/To/Data')
for file in directory.glob('data_*.csv'):
if file != 'data_Mon_*.csv':
os.remove(file)]
Answers:
if file != 'data_Mon_*.csv'
There’s two problems here:
file
is compared against the string 'data_Mon_*.csv'
. Since file
isn’t a string, these two objects will never be equal. So the if
condition will always be true. To fix this, you need to get the file’s name, rather than using the file object directly.
Even if you fix this, the string 'data_Mon_*.csv'
is literal. In other words, the *
is a *
. Unlike directory.glob('data_*.csv')
, this will only match a *
rather than match "anything" as in a glob expression. In order to fix this, you need to use a regular expression to match against your file name.
if all Monday files start with "data_Mon_" then you might use str.startswith:
def file_delete():
directory = pathlib.Path('/Path/To/Data')
for file in directory.glob('data_*.csv'):
if not file.name.startswith('data_Mon_'):
os.remove(file)
I have a directory with files like: data_Mon_15-8-22.csv
, data_Tue_16-8-22.csv
, data_Mon_22-8-22.csv
etc and I am trying to delete all but the Monday files. However, my script doesn’t seem to differentiate between the filenames and just deletes everything despite me stating it. Where did I go wrong? Any help would be much appreciated!
My Code:
def file_delete():
directory = pathlib.Path('/Path/To/Data')
for file in directory.glob('data_*.csv'):
if file != 'data_Mon_*.csv':
os.remove(file)]
if file != 'data_Mon_*.csv'
There’s two problems here:
file
is compared against the string 'data_Mon_*.csv'
. Since file
isn’t a string, these two objects will never be equal. So the if
condition will always be true. To fix this, you need to get the file’s name, rather than using the file object directly.
Even if you fix this, the string 'data_Mon_*.csv'
is literal. In other words, the *
is a *
. Unlike directory.glob('data_*.csv')
, this will only match a *
rather than match "anything" as in a glob expression. In order to fix this, you need to use a regular expression to match against your file name.
if all Monday files start with "data_Mon_" then you might use str.startswith:
def file_delete():
directory = pathlib.Path('/Path/To/Data')
for file in directory.glob('data_*.csv'):
if not file.name.startswith('data_Mon_'):
os.remove(file)