In your shell, the initial CWD setting varies by system. Therefore, you can refer to a file in a script by its name only provided that the file and the script are in the same directory. When you execute your script, your CWD is set to the directory where your script is. To complicate the matter, your Python has different initial CWD settings depending on whether you are working with a Python script or in a shell environment. Basically, referring to a file without specifying its path ( 'myfile.txt') works only when the file is in your CWD. The concept of Current Working Directory (CWD) is crucial here. How do I find its full file path and name? I have this file sitting in my Desktop/My Documents.That way, everything in the string is interpreted as a literal character, and you don't have to escape every backslash. Alternatively, you can prefix the entire file name string with the rawstring marker "r": r'C:\Users\narae\Desktop\foo.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/foo.txt'. Python simply lets you use OS-X/Linux style slashes "/" even in Windows.In Python, Windows files can be referred to in multiple ways: They start with the disk label "C:", and backslashes are used to separate directories. Windows file paths look like this: C:\Users\narae\Desktop\foo.txt. They start from the root "/", and slashes are used to separate directories. OS-X/Linux file paths look like this: /Users/narae/Desktop/foo.txt.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |