3

I've searched through many answers on deleting multiple files based on certain parameters (e.g. all txt files). Unfortunately, I haven't seen anything where one has a longish list of files saved to a .txt (or .csv) file and wants to use that list to delete files from the working directory.

I have my current working directory set to where the .txt file is (text file with list of files for deletion, one on each row) as well as the ~4000 .xlsx files. Of the xlsx files, there are ~3000 I want to delete (listed in the .txt file).

This is what I have done so far:

import os
path = "c:\\Users\\SFMe\\Desktop\\DeleteFolder"
os.chdir(path)
list = open('DeleteFiles.txt')
for f in list:
   os.remove(f)

This gives me the error:

OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: 'Test1.xlsx\n'

I feel like I'm missing something simple. Any help would be greatly appreciated!

Thanks

1
  • 1
    You need to provide the full path when you do os.remove Commented Oct 11, 2018 at 4:40

2 Answers 2

8
  1. Strip ending '\n' from each line read from the text file;
  2. Make absolute path by joining path with the file name;
  3. Do not overwrite Python types (i.e., in you case list);
  4. Close the text file or use with open('DeleteFiles.txt') as flist.

EDIT: Actually, upon looking at your code, due to os.chdir(path), second point may not be necessary.


import os
path = "c:\\Users\\SFMe\\Desktop\\DeleteFolder"
os.chdir(path)
flist = open('DeleteFiles.txt')
for f in flist:
    fname = f.rstrip() # or depending on situation: f.rstrip('\n')
    # or, if you get rid of os.chdir(path) above,
    # fname = os.path.join(path, f.rstrip())
    if os.path.isfile(fname): # this makes the code more robust
        os.remove(fname)

# also, don't forget to close the text file:
flist.close()
Sign up to request clarification or add additional context in comments.

1 Comment

Changing my original 'os.remove(f)' for 'os.remove(os.path.join(path, f.strip()))' did the trick. And good reminder for not overwriting list. Thanks so much for help!
3

As Henry Yik pointed in the commentary, you need to pass the full path when using os.remove function. Also, open function just returns the file object. You need to read the lines from the file. And don't forget to close the file. A solution would be:

import os
path = "c:\\Users\\SFMe\\Desktop\\DeleteFolder"
os.chdir(path)
# added the argument "r" to indicates only reading
list_file = open('DeleteFiles.txt', "r") 
# changing variable list to _list to do not shadow 
# the built-in function and type list
_list = list_file.read().splitlines() 
list_file.close()
for f in _list:
   os.remove(os.path.join(path,f))

A further improvement would be use list comprehension instead of a loop and a with block, which "automagically" closes the file for us:

with open('DeleteFiles.txt', "r") as list_file:
    _list = list_file.read().splitlines()
    [os.remove(os.path.join(path,f)) for f in _list]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.