

But if your file reference does not start from the top (e.g., 'alice.txt', 'ling1330/alice.txt'), Python assumes that it starts in the CWD (a " relative path"). So far we have been using the absolute path, which begins from the topmost directory. You can think of it as the folder your Python is operating inside at the moment. The concept of Current Working Directory (CWD) is crucial here.

However, you might have seen files called by their name only, e.g., 'alice.txt' in Python.
#PYTHON 3 TEXT FILES FOR WINDOWS AND MAC FULL#
So, using the full directory path and file name always works you should be using this method. That way, everything in the string is interpreted as a literal character, and you don't have to escape every backslash.įile Name Shortcuts and CWD (Current Working Directory) Alternatively, you can prefix the entire file name string with the rawstring marker "r": r'C:\Users\narae\Desktop\alice.txt'.If using backslash, because it is a special character in Python, you must remember to escape every instance: 'C:\\Users.

Therefore, you can refer to the file as 'C:/Users/narae/Desktop/alice.txt'.
