I am trying to read files, inside a folder, reason is the number of files inside the folder is not fixed, but if there are 3 text folder, I have to read all the 3 files if 4 all the 4 text files.
Here is the code I'm trying to use, but comes up with an IOError:
for i in os.listdir("./RecordFolder"):
print i
Output is:
record1.txt
record2.txt
Now the problem is reading the files:
for files in os.listdir("./RecordFolder"):
filecontent = open(files).readlines()
for lines in filecontent:
print lines
Output:
IOError: [Errno 2] No such file or directory: 'record.txt'
Need some help here, thanks
The function os.listdir() only returns the file names, not the full paths, so you should use os.path.join() to add the directory names:
directory = "./RecordFolder"
for filename in os.listdir(directory):
with open(os.path.join(directory, filename)) as f:
for line in f:
print line
(Also note that you shouldn't use file.readlines() for simply iterating over the lines of the files, and that your code fails to close the file. These problems are fixed in the code above.)
os.listdir only returns the name of the files inside the folder. Not the entire path.
You want something like:
directory = os.path.join(os.curdir, "RecordFolder")
for files in os.listdir(directory):
filecontent = open( os.path.join(directory, files) ).readlines()
for lines in filecontent:
print lines
But this isn't the best way to get the lines from a file. Look into python context managers -- specifically:
with open(filename) as f:
for line in f:
print line
This makes sure that the file is properly closed (even in the event of an error -- barring a segmentation fault in the interpreter itself or something bizarre like that).
Related
I have a folder full of csv files that contain results for different participants in an experiment. I'm trying to create one large csv file containing all the csv files in the directory. I'm using listdir() to create a list of all the files but I haven't been able to open the individual files in this list. I think I need to loop over each file but I haven't been able to figure out how.
This is the code I've come up with so far. I get the following error: FileNotFoundError: [Errno 2] No such file or directory: 'results_262.csv' because it appears that the loop only reads one file in the files variable even though there should be many.
from os import listdir
path = "results"
files = listdir(path)
print(files)
results = open("results.csv", "w")
data = []
for file in files:
participant = open(f"{file}", "r")
data.append(participant.readlines())
Would anyone be able to help?
Your issue is that listdir() returns the filenames without their path information, so you need to append the filename to the path.
import os
...
for file in files:
participant = open(os.join(path, file), "r"):
...
Other details:
f"{file}" is the same thing as just file. I.e., open(file, "r") is equivalent to open(f"{file}", "r"), but more efficient - there is not need to use interpolation what you already have the value you want in a variable.
And, you're not closing your files. You should add participant.close() in your loop, or, even better, use a context manager:
for file in files:
with open(os.join(path, file), "r") as participant:
...
The with puts your file handle in a context manager, and the file gets closed automatically when you leave the scope of the context manager.
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
Strip ending '\n' from each line read from the text file;
Make absolute path by joining path with the file name;
Do not overwrite Python types (i.e., in you case list);
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()
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]
I have been trying to write some python code in order to get each line from a .txt file and search for a file with that name in a folder and its subfolders. After this I want to copy that file in a preset destination folder.
The thing is when I test this code I can read all the files in the .txt and I can display all files in a directory and its subdirectories. The problem rises when I have to compare the filename I read from the .txt (line by line as I said) with all the filenames within the directory folder and then copy the file there.
Any ideas what am I doing wrong?
import os, shutil
def main():
dst = '/Users/jorjis/Desktop/new'
f = open('/Users/jorjis/Desktop/articles.txt', 'rb')
lines = [line[:-1] for line in f]
for files in os.walk("/Users/jorjis/Desktop/folder/"):
for line in lines:
if line == files:
shutil.copy('/dir/file.ext', '/new/dir')
You are comparing the file names from the text file with a tuple with three elements: the root path of the currently visited folder, a list of all subdirectory names in that path, and a list of all file names in that path. Comparing a string with a tuple will never be true. You have to compare each file name with the set of file names to copy. The data type set comes in handy here.
Opening a file together with the with statement ensures that it is closed when the control flow leaves the with block.
The code might look like this:
import os
import shutil
def main():
destination = '/Users/jorjis/Desktop/new'
with open('/Users/jorjis/Desktop/articles.txt', 'r') as lines:
filenames_to_copy = set(line.rstrip() for line in lines)
for root, _, filenames in os.walk('/Users/jorjis/Desktop/folder/'):
for filename in filenames:
if filename in filenames_to_copy:
shutil.copy(os.path.join(root, filename), destination)
If I had to guess, I would say that the files in the .txt contain the entire path. You'd need to add a little more to os.walk to match up completely.
for root, _, files in os.walk("/Users/jorjis/Desktop/folder/"):
for f in files:
new_path = f + root
if new_path in lines:
shutil.copy(new_path, `/some_new_dir')
Then again, I'm not sure what the .txt file looks like so it might be that your original way works. If that's the case, take a closer look at the lines = ... line.
I have this code which lists the files in the directory and parses each one of them with my function.
paths = []
for filename in os.listdir(r"C:\Program Files (x86)\Folder\Folder"):
with open(filename) as f:
paths.append(parse_file(f))
I am getting the error:
File "find.py", line 21, in <module>
with open(filename) as f:
IOError: [Errno 2] No such file or directory: 'file.txt'
This error shows that it saw file.txt because it exist in the folder I specified in os.listdir, I have many more files there. If I delete file.txt it will show the error on a different file.
I also tried to move the files to a directory in my desktop and the script worked fine.
What is the issue I don't understand. I am pretty new to python so forgive me if its dumb question. Thank you!
os.listdir() returns filenames, not paths. Join them with the directory name to make absolute paths:
path = r"C:\Program Files (x86)\Folder\Folder"
for filename in os.listdir(path):
with open(os.path.join(path, filename)) as f:
I have this code which lists the files in the directory and parses each one of them with my function.
paths = []
for filename in os.listdir(r"C:\Program Files (x86)\Folder\Folder"):
with open(filename) as f:
paths.append(parse_file(f))
I am getting the error:
File "find.py", line 21, in <module>
with open(filename) as f:
IOError: [Errno 2] No such file or directory: 'file.txt'
This error shows that it saw file.txt because it exist in the folder I specified in os.listdir, I have many more files there. If I delete file.txt it will show the error on a different file.
I also tried to move the files to a directory in my desktop and the script worked fine.
What is the issue I don't understand. I am pretty new to python so forgive me if its dumb question. Thank you!
os.listdir() returns filenames, not paths. Join them with the directory name to make absolute paths:
path = r"C:\Program Files (x86)\Folder\Folder"
for filename in os.listdir(path):
with open(os.path.join(path, filename)) as f: