英文:
How to combine files in python 3 - with open() as: NOT working
问题
抱歉,你的代码中存在一些HTML转义字符,我将提供一个不含HTML转义字符的中文翻译:
我正在尝试将存储在位于比下面的.py文件所在目录的上一级目录中的所有.txt文件的内容合并在一起。
import os
def main():
# 获取存储在此.py文件的上一级目录中的文件列表
folder = os.listdir("../notes")
# 合并这些文件
merge_files(folder)
def merge_files(folder):
# 创建一个列表来存储文件名
files_names = []
# 以追加模式打开输出文件
with open("merged_notes.txt", "a") as outfile:
# 遍历文件列表
for file in folder:
# 将文件名添加到列表
files_names.append(file)
print(files_names)
# 以读取模式打开输入文件
with open(file, "r") as infile:
# 从输入文件中读取数据
data = infile.read()
# 将数据写入输出文件(文件名、数据、换行符)
outfile.write(file)
outfile.write(data)
outfile.write("\n")
# 返回合并后的文件名
return "merged_notes.txt"
if __name__ == "__main__":
main()
我一直收到这个错误:
FileNotFoundError: [Errno 2] No such file or directory: 'File bard Mar 30 2023, 4 30 48 PM.txt'
然而,文件名确实保存在files_names
列表中,这意味着for循环确实在“notes”目录中找到了文件。我不明白为什么with open(file, 'r')
不起作用。
英文:
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.
答案1
得分: 1
当您尝试遍历文件夹时,您只获取到文件名,但没有提供文件存储的路径,这就是为什么它不起作用的原因,因为文件位于根文件夹以外的不同文件夹中,您必须提供文件路径。
英文:
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.
答案2
得分: 1
从os.listdir("../notes")
获得的文件名是相对于../notes
目录的,而不是当前目录。您需要使用正确的路径前缀文件名。
尝试使用pathlib
,它提供了更多的自动功能:
from pathlib import Path
notes = Path("../notes").iterdir()
for note in notes:
with open(note) as f:
data = f.read()
print(data) # 文件的内容
英文:
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
答案3
得分: 1
open()
函数期望一个文件路径,但你的代码中file
只是文件名,没有包含文件所在目录的路径。
在print(file_names)
后面添加以下行:
file_path = os.path.join("../notes", file)
并将open()
函数的参数更改为file_path
:
with open(file_path, "r") as infile:
英文:
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:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论