How to combine files in python 3 – with open() as: NOT working
Question:
I am trying to combine the content of all .txt files in a directory one level above the directory where the .py file below is stored.
import os
def main():
# Get list of files stored in a dir above this .py's dir
folder = os.listdir("../notes")
# Merge the files
merge_files(folder)
def merge_files(folder):
# Create a list to store the names of the files
files_names = []
# Open output file in append mode
with open("merged_notes.txt", "a") as outfile:
# Iterate through the list of files
for file in folder:
# Add name of file to list
files_names.append(file)
print(files_names)
# Open input file in read mode
with open(file, "r") as infile:
# Read data from input file
data = infile.read()
# Write data to output file (file name, data, new line)
outfile.write(file)
outfile.write(data)
outfile.write("n")
# Return merged file
return "merged_notes.txt"
if __name__ == "__main__":
main()
I keep getting this error:
FileNotFoundError: [Errno 2] No such file or directory: ‘File bard Mar 30 2023, 4 30 48 PM.txt’
However, the file name is saved in the list files_names
, which means the for loop does find the file in the "notes" directory. I don’t understand why with open(file, 'r')
doesn’t.
Answers:
When you are trying to loop through the folder you are getting only the file names, but you are not providing the path where the file is stored,that’s why its not working as the files are in a different folder than the root folder,you have to give the file path.
The file names that you get from os.listdir("../notes")
is relative to the ../notes
directory, not the current directory. You need to prefix the file name with the correct path.
Try using pathlib
, which gives you some more automatic stuff:
from pathlib import Path
notes = Pathlib("../notes").iterdir()
for note in notes:
with open(note) as f:
data = f.read()
print(data) # contents of the file
The open() function expects a file path, but file in your code is just the file name without path to directory where file is location.
Add the following line right after print(file_names):
file_path = os.path.join("../notes", file)
And change the open() funtion to take in file_path:
with open(file_path, "r") as infile:
I am trying to combine the content of all .txt files in a directory one level above the directory where the .py file below is stored.
import os
def main():
# Get list of files stored in a dir above this .py's dir
folder = os.listdir("../notes")
# Merge the files
merge_files(folder)
def merge_files(folder):
# Create a list to store the names of the files
files_names = []
# Open output file in append mode
with open("merged_notes.txt", "a") as outfile:
# Iterate through the list of files
for file in folder:
# Add name of file to list
files_names.append(file)
print(files_names)
# Open input file in read mode
with open(file, "r") as infile:
# Read data from input file
data = infile.read()
# Write data to output file (file name, data, new line)
outfile.write(file)
outfile.write(data)
outfile.write("n")
# Return merged file
return "merged_notes.txt"
if __name__ == "__main__":
main()
I keep getting this error:
FileNotFoundError: [Errno 2] No such file or directory: ‘File bard Mar 30 2023, 4 30 48 PM.txt’
However, the file name is saved in the list files_names
, which means the for loop does find the file in the "notes" directory. I don’t understand why with open(file, 'r')
doesn’t.
When you are trying to loop through the folder you are getting only the file names, but you are not providing the path where the file is stored,that’s why its not working as the files are in a different folder than the root folder,you have to give the file path.
The file names that you get from os.listdir("../notes")
is relative to the ../notes
directory, not the current directory. You need to prefix the file name with the correct path.
Try using pathlib
, which gives you some more automatic stuff:
from pathlib import Path
notes = Pathlib("../notes").iterdir()
for note in notes:
with open(note) as f:
data = f.read()
print(data) # contents of the file
The open() function expects a file path, but file in your code is just the file name without path to directory where file is location.
Add the following line right after print(file_names):
file_path = os.path.join("../notes", file)
And change the open() funtion to take in file_path:
with open(file_path, "r") as infile: