Python Forum

Full Version: Accessing files in various directories and folders
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello all,

First time on this forum and new to Python (which I am studying in my old age specifically for data analysis and machine learning).

I got myself a couple of courses to get started. I am working my way through a data visualisation with matplotlib using the anaconda install and the Spyder editor (as is prescribed in the course). I am doing fine with the actual code and examples but I am really lost when it comes to referring to files outside the immediate directory (C:\Users\name\.spyder-py3 in my case. The course is great bu the instructor keeps accessing files using commands such as
with open("File.txt","r") as shortname
in which File.txt is assumed to be in the local directory. I did copy the files I need to access in the local directory described above, out of sheer frustration, but I just couldn't couldn't access them on remote directories.

For example, I have the course of an external hard dish mapped to say, G:\. The full path of the file is, say G:\Python course\Data Files\File.txt . Using that full path in a Spyder command line yields and error. I don't seem to find the right syntax for accessing the file using the full path (maybe spaces are an issue, maybe it just can't be done like that...I just don't know).

Sorry for this very basic question, but if someone could give me some pointers, that would be brilliant.

Thank you all

Chris
when working with file paths, use raw strings or forward slash, because backslash is the escape char and in combination with some symbols has special meaning. in this particular case the problem is '\F'
r'G:\Python course\Data Files\File.txt' or 'G:/Python course/Data Files/File.txt' would do
That was so quick! Thank you very much, buran.