我在一个文件site-packages
名为目录wordlist.py
是只包含一个行:
f = open("words.txt")
words.txt
在同一目录中有一个称为的文件。当我运行时,wordlist.py
它工作正常。但是,每当我使用时import wordlist
,都会出现错误:
FileNotFoundError: [Errno 2] No such file or directory: 'words.txt'
我正在使用IDLE for Python 3.4
如果您使用相对路径作为文件名或目录名,则python会在当前工作目录(bash中的$ PWD变量)中查找(或创建)它们。
如果您想让它们相对于当前的python文件,可以使用(python 3.4)
import pathlib
HERE = Path(__file__).parent.absolute()
WORDS_PATH = HERE / '../path/to/words.txt'
with WORDS_PATH.open() as file_pointer:
'do something with file_pointer...'
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句