Python On Windows: IOError: [Errno 2] No Such File Or Directory
First of all, I'm very new to Python and programming in general. Currently I'm trying to create a script that will remove all files with random names, extensions and content in the
Solution 1:
os.listdir
returns filenames, not complete path.
PATH = "C:/path/to/files/"
for filename in os.listdir(PATH):
content = open(os.path.join(PATH, filename)).read()
Here, os.path.join
is used for merge the path and the filename.
Solution 2:
if you want to remove the file, it should pass the whole path of file.
import os
black_list = [line for line in open("C:/path/to/blacklist.txt")]
path = "C:/path/to/files/"
for filename in os.listdir(path):
content = open(path+ filename).read()
if any(line in content for line in black_list):
os.remove(path + filename)
Post a Comment for "Python On Windows: IOError: [Errno 2] No Such File Or Directory"