On 01/10/2012 04:53 PM, Hugo Arts wrote:
On Tue, Jan 10, 2012 at 8:31 PM, Adrian<kellyadr...@hotmail.com>  wrote:
Hi guys,
I know that if i dont include any path information, python looks in the current 
directory for the file. My question is how do i specify a file path to open a 
file saved on my desktop for example.

Thanks all

Adrian

Just write the path like you would anywhere else, there is nothing
special about how python handles this.

# this is where my desktop is located on my windows 7 machine, but it
differs per operating system of course
f = open("C:\Users\hugo\Desktop\file.txt", 'r')

You'd want to do one of three things there:

1) use forward slashes
                   "C:/Users/hugo/Desktop/file.txt"
      which Windows will use happily for nearly every purpose.

2) use raw strings
                  r"C:\Users\hugo\Desktop\file.txt"

3) or escape the backslashes:
                    "C:\\Users\\hugo\\Desktop\\file.txt"

# you can also use relative paths, like "two directories up from the
current and then into the media directory"
# forward slashes here, that's what they use on essentially everything
that isn't windows
f = open("../../media/file.txt", 'r')

if you want to be cross-platform, you should take a look at the os.path module.

HTH,
Hugo
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor



--

DaveA

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to