How to read a file in other directory in python

前端 未结 8 1245
暖寄归人
暖寄归人 2020-12-13 13:46

I have a file named 5_1.txt in a directory named direct, how can I read that file using read?

I verified the path using :

8条回答
  •  粉色の甜心
    2020-12-13 14:15

    In case you're not in the specified directory (i.e. direct), you should use (in linux):

    x_file = open('path/to/direct/filename.txt')
    

    Note the quotes and the relative path to the directory.

    This may be your problem, but you also don't have permission to access that file. Maybe you're trying to open it as another user.

提交回复
热议问题