How to read a file in other directory in python

前端 未结 8 1243
暖寄归人
暖寄归人 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:13

    For folks like me looking at the accepted answer, and not understanding why it's not working, you need to add quotes around your sub directory, in the green checked example,

    x_file = open(os.path.join(direct, "5_1.txt"), "r")  
    

    should actually be

    x_file = open(os.path.join('direct', "5_1.txt"), "r")   
    

提交回复
热议问题