[英]python text file in folder within the directory folder not found
I'm running my code in command prompt from the directory * FOLDER "Game" within "Game" I have other FOLDERS to keep it neat and easier to navigate/find files, inside a folder "Text" I have a Text file named "Note8.txt" so the text file "Note8.txt" is in folder "Text" inside the directory folder "Game" but while running my code the command prompt states error could not find "Note8" in the directory I took "Note8.txt" out of its folder and it worked, I assumed having folders within the directory folder would automatically make them subfiles so I could still use我在命令提示符下从目录运行我的代码 *“游戏”中的文件夹“游戏”我有其他文件夹以保持整洁,更容易导航/查找文件,在文件夹“文本”内我有一个名为“的文本文件” Note8.txt”,因此文本文件“Note8.txt”位于“Game”目录文件夹内的“Text”文件夹中,但在运行我的代码时,命令提示符指出错误在我使用“Note8.txt”的目录中找不到“Note8” 。 txt”从它的文件夹中取出并且它工作,我认为在目录文件夹中有文件夹会自动使它们成为子文件,所以我仍然可以使用
with open("Note8.txt",'r') as Note8txt:
Note8 = read(Note8txt.read())
this works fine with the text file in the folder but not when the text files are inside the folder, is there a way to get this to work or dose all my files need to be inside the folder and not inside folders within the folder?这适用于文件夹中的文本文件,但当文本文件在文件夹内时,有没有办法让它工作或剂量我的所有文件都需要在文件夹内而不是文件夹内的文件夹内?
Try This尝试这个
with open('TextFiles/Note8.txt', encoding='utf-8', mode='r') as file:
print(file.read())
file.close()
# OR SOME OTHER CODE
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.