如何解决这个问题:python无法读取我的文件

roqulrg3  于 2021-08-25  发布在  Java
关注(0)|答案(1)|浏览(350)
with open('Passwords.txt') as passwords:
    for line in passwords:
        print(line)

我刚刚输入了这个代码,它给了我

FileNotFoundError: [Errno 2] No such file or directory: 'Passwords.txt'

我确信这个文件和我把python文件放在同一个文件夹中。这就是证据:文件在同一个文件夹中

rta7y2nd

rta7y2nd1#

您应该首先输入完整路径,下面是一个示例:

with open(r'/home/user/Documents/Passwords.txt') as passwords:
    # and don't forget this
    content = passwords.readlines()
    for line in passwords:
        print(line)

相关问题