Python Forum
Accessing files in various directories and folders
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Accessing files in various directories and folders
#1
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
Reply
#2
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
That was so quick! Thank you very much, buran.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Using zipfile module - finding folders not files darter1010 2 246 Apr-06-2024, 07:22 AM
Last Post: Pedroski55
  Organization of project directories wotoko 3 419 Mar-02-2024, 03:34 PM
Last Post: Larz60+
  Create new folders and copy files cocobolli 3 1,440 Mar-22-2023, 10:23 AM
Last Post: Gribouillis
  Copy only hidden files and folders with rsync Cannondale 2 997 Mar-04-2023, 02:48 PM
Last Post: Cannondale
  Listing directories (as a text file) kiwi99 1 834 Feb-17-2023, 12:58 PM
Last Post: Larz60+
  Find duplicate files in multiple directories Pavel_47 9 3,068 Dec-27-2022, 04:47 PM
Last Post: deanhystad
  rename same file names in different directories elnk 0 710 Nov-04-2022, 05:23 PM
Last Post: elnk
  I need to copy all the directories that do not match the pattern tester_V 7 2,408 Feb-04-2022, 06:26 PM
Last Post: tester_V
  Functions to consider for file renaming and moving around directories cubangt 2 1,744 Jan-07-2022, 02:16 PM
Last Post: cubangt
  Moving files to Folders giddyhead 13 9,123 Mar-07-2021, 02:50 AM
Last Post: giddyhead

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020