我是编码的绝对新手,非常抱歉,如果这是一个非常基本的问题。我试图打开不同目录中的特定文件(从当前正在工作的那个目录中)并使其执行某些操作。当我只是要求它找到这些文件时,它做得很好,但它不会打开这些文件(“FileNotFoundError:[Errno 2]没有这样的文件或目录”)Iam不知道我出错的地方。这里是我的代码:在python3中反复打开不同目录中的文件osx
for file_name in os.listdir("/Users/sujathakoduvayurp/Downloads/exercises-and-examples/Chapter 9/exercises"):
if file_name.endswith(".dna"):
print (file_name)
这没问题,我得到的文件列表的右扩展名。像这样: xaa.dna xab.dna xac.dna xad.dna xae.dna xaf.dna xag.dna xah.dna xai.dna xaj.dna
但是,当我请执行以下操作:
for file_name in os.listdir("/Users/sujathakoduvayurp/Downloads/exercises-and-examples/Chapter 9/exercises"):
if file_name.endswith(".dna"):
dna_file= open("/Users/sujathakoduvayurp/Downloads/exercises-and-examples/Chapter 9/file_name","r")
我收到如前所述的错误消息。我不确定问题是什么。任何帮助将不胜感激。 谢谢! PS:我不确定它是否在这里正确出来,但我的缩进是好的。