10 Replies - 1103 Views - Last Post: 25 January 2012 - 05:42 AM Rate Topic: -----

#1 praditmodi  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 30-November 11

Python File error

Posted 24 January 2012 - 05:39 PM

Posted Image
Why is this happening?
f = open('c:\ndfa.txt')
shows an error while
 f = open ('c:\log.txt')
executes both the files are in the same drive ?
Is This A Good Question/Topic? 0
  • +

Replies To: Python File error

#2 Vblaster  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 16-January 11

Re: Python File error

Posted 24 January 2012 - 06:37 PM

In your directory screenshot the files are log and ndfa, not log.txt and ndfa.txt

Try removing the .txt extension from the open command.
Was This Post Helpful? 0
  • +
  • -

#3 praditmodi  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 30-November 11

Re: Python File error

Posted 24 January 2012 - 06:52 PM

neither of them executes if i remove the .txt extension
Was This Post Helpful? 0
  • +
  • -

#4 Vblaster  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 16-January 11

Re: Python File error

Posted 24 January 2012 - 07:55 PM

Try it with a mode argument eg. f = open('log.txt', 'r') for read or f = open('log.txt', 'w') for write.
Was This Post Helpful? 0
  • +
  • -

#5 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Python File error

Posted 24 January 2012 - 08:07 PM

When using backslashes, such as in your pathname, you need to escape them from the interpreter:

Escaping the backslash:
f = open('C:\\myfile.txt')



Using a raw string:
f = open(r'C:\myfile.txt')



You don't need to use an absolute path when specifying a filename, a relative path will work fine, especially if the file is "close".

This post has been edited by Simown: 24 January 2012 - 08:10 PM

Was This Post Helpful? 2
  • +
  • -

#6 Vblaster  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 16-January 11

Re: Python File error

Posted 24 January 2012 - 08:11 PM

My Bad. Is that a windows problem only?( linux uses forward slashes :sweatdrop: )?
Was This Post Helpful? 0
  • +
  • -

#7 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Python File error

Posted 24 January 2012 - 08:23 PM

It's not a windows only problem, but by using forward slashes/back slashes, you risk making the program bound to a single operating system. A more elegant solution would be to use os.path.join() to construct a path, in the os module of the Python standard library. That way your program would build the directory to your file OS-independent, so, it will run on Windows and Linux.

This post has been edited by Simown: 24 January 2012 - 08:24 PM

Was This Post Helpful? 1
  • +
  • -

#8 praditmodi  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 30-November 11

Re: Python File error

Posted 24 January 2012 - 08:26 PM

f = open(r'C:\ndfa.txt')
Thanks Simown it did work; and what was the thing you said about escaping the backslash ?
what i understood was that in order to escape from the backslash we should raw string ?
Was This Post Helpful? 0
  • +
  • -

#9 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Python File error

Posted 24 January 2012 - 08:46 PM

They are both ways of escaping backslashes, an escaped backslash tells Python to treat it as a backlash, not some other special character with a different semantic meaning.

Consider the path:
'A\B\C\D....\Z\'


That could be:
'A\\B\\C\\D....\\Z\\'


Or:
r'A\B\C\D....\Z\'


The previous two examples mean exactly the same thing!

No, you don't need to escape it AND have a raw string, you can use one or the other - it's your personal preference.

It can get cluttered if you have more escaped characters in a string, I prefer to use raw strings most of the time myself.

This post has been edited by Simown: 24 January 2012 - 08:49 PM

Was This Post Helpful? 1
  • +
  • -

#10 praditmodi  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 30-November 11

Re: Python File error

Posted 24 January 2012 - 09:30 PM

Thank You Simown ! Helped a lot !
Was This Post Helpful? 0
  • +
  • -

#11 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Python File error

Posted 25 January 2012 - 05:42 AM

You're welcome :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1