简体   繁体   English

python 目录中文件夹中的文本文件未找到

[英]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.

相关问题 如果在文本文件中找到特定的字符串,则从文件夹内读取文本文件并保存文件夹的名称-Python - Reading a text file from within a folder and saving the name of the folder if a particular string is found in the text file - Python 使用python脚本从文本文件创建文件夹目录 - Making a folder directory from a text file using a python script 向上移动目录,直到找到文件夹 python - Move up directory until folder found python Python-读取另一个文件夹/目录中的文本文件 - Python - Reading text files in another folder/directory Zip 多个文件夹(在目录中)到单个文件(在新目录中),在 Python - Zip multiple folder (within directory) to individual files (in a new directory), in Python 使用Selenium python将文件下载到所需文件夹或当前测试目录文件夹中 - Download the file with Selenium python in a desired folder or current tests directory folder 使用 python 获取位于父目录的子文件夹中的文件 - Get a file that is in a folder that is a child of the parent directory with python Python - 在文件夹中导入文件 - 找不到模块 - Python - Import file inside folder - No module found Python - 如果找到文件,则获取列表中的文件夹数 - Python - get number of folder in list if file was found 如何使用python检测文件夹中的文件? - How to detect file within folder with python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM