[Tutor] Defining a File path
Hugo Arts
hugo.yoshi at gmail.com
Wed Jan 11 14:14:06 CET 2012
On Tue, Jan 10, 2012 at 11:24 PM, Dave Angel <d at davea.name> wrote:
> On 01/10/2012 04:53 PM, Hugo Arts wrote:
>>
>> 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'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"
>
whoops. think before you type. Thanks for the correction
More information about the Tutor
mailing list