Problem opening a text file in Python

后端 未结 2 1812
孤独总比滥情好
孤独总比滥情好 2021-02-10 01:49

This seems like it should be very easy:

f = open(\'C:\\Users\\john\\Desktop\\text.txt\', \'r\')

But I am getting this error:

           


        
2条回答
  •  孤独总比滥情好
    2021-02-10 02:43

    Your file name has backslash characters in it. Backslash is the escape character in Python strings. Either replace them with '/' characters or use r'C:\Users\john\Desktop\text.txt'.

    You might also find the functions in os.path useful.

提交回复
热议问题