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.
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: