[Tutor] Defining a File path
Hugo Arts
hugo.yoshi at gmail.com
Tue Jan 10 22:53:36 CET 2012
On Tue, Jan 10, 2012 at 8:31 PM, Adrian <kellyadrian at 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 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
More information about the Tutor
mailing list