[BangPypers] Doubt regarding python file management

Noufal Ibrahim noufal at gmail.com
Tue Jan 18 13:32:18 CET 2011

On Tue, Jan 18 2011, Harish S wrote:

> On Tue, Jan 18, 2011 at 5:39 PM, Harish S <harishisnow at gmail.com> wrote:
>> Hello,
>>   I am new to python. I have a doubt.
>>   How will I open a file which is placed in a different directory (other
>> than the current working directory)?
>>   Wen I try to open the file using the command "file =
>> open("d:\desktop\....")", it says :
>>   IOError: [Errno 22] invalid mode <'r'> or filename: '......'
>> Can you please specify the reason for this?


On windows, the backslash used to separate directories can be
misinterpreted as escape sequences. So you'd best do

   f = open(r"d:\Desktop\MyFile.txt")

The r before the " indicates that the string is "raw" and that things
like \n, \t etc. should not be interpreted as special characters
(newline, tab etc.)

Also, what is "......."? Is that your filename? Is that a legal filename
under Windows?


More information about the BangPypers mailing list