python 2.5 - f=open('a_file.txt','w') gives [Errno 2]

Discussion in 'Python' started by dirkheld, Dec 3, 2007.

  1. dirkheld

    dirkheld Guest

    Hi,

    I upgraded my system from tiger to leopard. With leopard came a new
    version of python 2.5

    Now I trying to run some python code that previously worked on tiger
    (which included an older version of python).

    This is the error I'm receiving :

    Traceback (most recent call last):
    File "test.py", line 18, in print_total
    f=open('a_file.txt','w')
    IOError: [Errno 2] No such file or directory: 'a_file.txt'


    Is this a bug in python? This code worked on my old system
     
    dirkheld, Dec 3, 2007
    #1
    1. Advertising

  2. dirkheld wrote:

    > Hi,
    >
    > I upgraded my system from tiger to leopard. With leopard came a new
    > version of python 2.5
    >
    > Now I trying to run some python code that previously worked on tiger
    > (which included an older version of python).
    >
    > This is the error I'm receiving :
    >
    > Traceback (most recent call last):
    > File "test.py", line 18, in print_total
    > f=open('a_file.txt','w')
    > IOError: [Errno 2] No such file or directory: 'a_file.txt'
    >
    >
    > Is this a bug in python? This code worked on my old system


    The obvious question is: do you have 'a_file.txt' on your new system? There
    are many happy python programmers on leopard, and this is one heck of a
    basic functionality - I really doubt it's broken in such obvious way (if
    any at all).

    Diez
     
    Diez B. Roggisch, Dec 3, 2007
    #2
    1. Advertising

  3. dirkheld

    Rick Dooling Guest

    On Dec 3, 7:47 am, dirkheld <> wrote:

    > IOError: [Errno 2] No such file or directory: 'a_file.txt'


    I sometimes see that error on Linux when trying to run a script with
    DOS line endings. Is it an imported file? I don't know Macs, but start
    by making sure both your script and the file have Mac line endings?

    rick
     
    Rick Dooling, Dec 3, 2007
    #3
  4. dirkheld

    dirkheld Guest

    On 3 dec, 14:54, "Diez B. Roggisch" <> wrote:
    > dirkheld wrote:
    > > Hi,

    >
    > > I upgraded my system from tiger to leopard. With leopard came a new
    > > version of python 2.5

    >
    > > Now I trying to run some python code that previously worked on tiger
    > > (which included an older version of python).

    >
    > > This is the error I'm receiving :

    >
    > > Traceback (most recent call last):
    > > File "test.py", line 18, in print_total
    > > f=open('a_file.txt','w')
    > > IOError: [Errno 2] No such file or directory: 'a_file.txt'

    >
    > > Is this a bug in python? This code worked on my old system

    >
    > The obvious question is: do you have 'a_file.txt' on your new system? There
    > are many happy python programmers on leopard, and this is one heck of a
    > basic functionality - I really doubt it's broken in such obvious way (if
    > any at all).
    >
    > Diez


    I don't have a file called 'a_file.txt'
    I want to create that file and write some data to it. (http://
    docs.python.org/tut/node9.html#SECTION009200000000000000000)
    The strange thing is that it worked under tiger with an older vesion
    of python without any problem....
     
    dirkheld, Dec 3, 2007
    #4
  5. dirkheld wrote:

    > On 3 dec, 14:54, "Diez B. Roggisch" <> wrote:
    >> dirkheld wrote:
    >> > Hi,

    >>
    >> > I upgraded my system from tiger to leopard. With leopard came a new
    >> > version of python 2.5

    >>
    >> > Now I trying to run some python code that previously worked on tiger
    >> > (which included an older version of python).

    >>
    >> > This is the error I'm receiving :

    >>
    >> > Traceback (most recent call last):
    >> > File "test.py", line 18, in print_total
    >> > f=open('a_file.txt','w')
    >> > IOError: [Errno 2] No such file or directory: 'a_file.txt'

    >>
    >> > Is this a bug in python? This code worked on my old system

    >>
    >> The obvious question is: do you have 'a_file.txt' on your new system?
    >> There are many happy python programmers on leopard, and this is one heck
    >> of a basic functionality - I really doubt it's broken in such obvious way
    >> (if any at all).
    >>
    >> Diez

    >
    > I don't have a file called 'a_file.txt'
    > I want to create that file and write some data to it. (http://
    > docs.python.org/tut/node9.html#SECTION009200000000000000000)
    > The strange thing is that it worked under tiger with an older vesion
    > of python without any problem....


    Oh sorry, my bad. I didn't see the mode flag. Do you by any chance happen to
    not have the right to create files in the directory you are? What happens
    if you use it in /tmp for example?

    Still, I hold up to my reasoning: writing files is so essential in python,
    and certainly not subject to great - if any - change (after all, it's just
    calling the underlying OS functions) that I doubt it is broken.

    Diez
     
    Diez B. Roggisch, Dec 3, 2007
    #5
  6. dirkheld <> writes:

    > I don't have a file called 'a_file.txt'
    > I want to create that file and write some data to it.


    How exactly are you starting the Python interpreter? "No such file or
    directory" on file creation can happen when you try to create a file
    in a directory that has ceased to exist:

    $ mkdir x
    $ cd x
    $ rm -rf ~/x
    $ python
    Python 2.5.1 (r251:54863, Oct 5 2007, 13:36:32)
    [GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> f = open('foo', 'w')

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    IOError: [Errno 2] No such file or directory: 'foo'
     
    Hrvoje Niksic, Dec 3, 2007
    #6
  7. dirkheld

    dirkheld Guest


    > How exactly are you starting the Python interpreter? "No such file or
    > directory" on file creation can happen when you try to create a file
    > in a directory that has ceased to exist:


    Damn...now it's time to be ashamed. Because of my new installation of
    leopard I used another computername. So the path were I wanted to
    write to didn't exist any more.

    Previous : /users/dirkheld/programming/python/....
    Now : /users/dirk/programming/python/...

    Sorry for keeping you guys busy with such a mistake ;)
     
    dirkheld, Dec 4, 2007
    #7
  8. dirkheld a écrit :
    >>How exactly are you starting the Python interpreter? "No such file or
    >>directory" on file creation can happen when you try to create a file
    >>in a directory that has ceased to exist:

    >
    >
    > Damn...now it's time to be ashamed. Because of my new installation of
    > leopard I used another computername. So the path were I wanted to
    > write to didn't exist any more.
    >
    > Previous : /users/dirkheld/programming/python/....
    > Now : /users/dirk/programming/python/...


    which should remind everybody that hard-coding a path is a bad idea !-)

    > Sorry for keeping you guys busy with such a mistake ;)
     
    Bruno Desthuilliers, Dec 4, 2007
    #8
  9. dirkheld

    Rick Dooling Guest

    > Sorry for keeping you guys busy with such a mistake ;)

    No apologies necessary, especially since you reported the final
    outcome. Now anybody searching on that message will find a complete
    thread and a lesson learned.

    rick
     
    Rick Dooling, Dec 5, 2007
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rolf Magnus
    Replies:
    2
    Views:
    605
    Karl Heinz Buchegger
    Nov 28, 2003
  2. Elie Nader
    Replies:
    1
    Views:
    641
  3. Sameen
    Replies:
    2
    Views:
    446
    Victor Bazarov
    Aug 29, 2005
  4. viza

    &errno, sizeof errno

    viza, Sep 12, 2008, in forum: C Programming
    Replies:
    20
    Views:
    1,000
    Tim Rentsch
    Sep 14, 2008
  5. Glenn Linderman

    errno 22 instead of errno 2

    Glenn Linderman, Jan 28, 2009, in forum: Python
    Replies:
    0
    Views:
    365
    Glenn Linderman
    Jan 28, 2009
Loading...

Share This Page