Re: Python script not working on windows 7 but works fine on linux

Discussion in 'Python' started by ian douglas, Mar 4, 2013.

  1. ian douglas

    ian douglas Guest

    On 03/04/2013 11:06 AM, io wrote:
    > esclusioni_file = open('/home/io/btc_trading/exclusions.txt','r')
    >
    > Windows error :
    >
    > Traceback (most recent call last):
    > File "C:\btc_trading\scripts
    > \import_json_2_csv_from_web_and_exclusions.py", line 10, in <module>
    > f = open("/home/io/btc_trading/markets.csv","wb")
    > IOError: [Errno 2] No such file or directory: '/home/io/btc_trading/
    > markets.csv'


    The error tells you everything you need to know: the file system has no
    path/file called "/home/io/btc_trading/markets.csv"

    I imagine your CSV file lives in a different location on your Windows 7
    system (which also uses back-slashes '\' instead of forward-slashes '/',
    so you'll need to maybe do a try/except around the open() call to open a
    Windows path like open("c:\\users\\io\\Documents\\markets.csv") or
    whatever. OR you'll need to do some OS detection ahead of time to set
    the file path properly.
    ian douglas, Mar 4, 2013
    #1
    1. Advertising

  2. On Tue, Mar 5, 2013 at 6:59 AM, io <> wrote:
    > Genius!
    >
    > The code i posted was an example.
    > My real code was c:\btc_trading ....
    >
    > i was just missing the double slashes!
    >
    > Thanks , thankyou very much.
    >
    > :)


    Even on Windows, you can use / as a directory separator. This
    generally saves you the trouble of switching to \\ for everything;
    though still, absolute paths are often wrong. But at least you can use
    relative paths safely!

    ChrisA
    Chris Angelico, Mar 4, 2013
    #2
    1. Advertising

  3. On Mon, 04 Mar 2013 11:20:28 -0800, ian douglas wrote:

    > The error tells you everything you need to know: the file system has no
    > path/file called "/home/io/btc_trading/markets.csv"
    >
    > I imagine your CSV file lives in a different location on your Windows 7
    > system (which also uses back-slashes '\' instead of forward-slashes '/',


    Windows understands forward slashes in paths too. You can make your code
    (almost) platform-independent, and avoid a lot of problems with unescaped
    backslashes, by always using forward slashes in paths.



    --
    Steven
    Steven D'Aprano, Mar 5, 2013
    #3
  4. ian douglas

    Andrew Berg Guest

    On 2013.03.04 19:58, Steven D'Aprano wrote:
    > Windows understands forward slashes in paths too. You can make your code
    > (almost) platform-independent, and avoid a lot of problems with unescaped
    > backslashes, by always using forward slashes in paths.

    Or use os.path.join, the entire purpose of which is to create suitable
    paths dynamically.

    --
    CPython 3.3.0 | Windows NT 6.2.9200 / FreeBSD 9.1
    Andrew Berg, Mar 5, 2013
    #4
  5. On Tuesday, March 5, 2013 3:20:28 AM UTC+8, ian douglas wrote:
    > On 03/04/2013 11:06 AM, io wrote:
    >
    > > esclusioni_file = open('/home/io/btc_trading/exclusions.txt','r')

    >
    > >

    >
    > > Windows error :

    >
    > >

    >
    > > Traceback (most recent call last):

    >
    > > File "C:\btc_trading\scripts

    >
    > > \import_json_2_csv_from_web_and_exclusions.py", line 10, in <module>

    >
    > > f = open("/home/io/btc_trading/markets.csv","wb")

    >
    > > IOError: [Errno 2] No such file or directory: '/home/io/btc_trading/

    >
    > > markets.csv'

    >
    >
    >
    > The error tells you everything you need to know: the file system has no
    >
    > path/file called "/home/io/btc_trading/markets.csv"
    >
    >
    >
    > I imagine your CSV file lives in a different location on your Windows 7
    >
    > system (which also uses back-slashes '\' instead of forward-slashes '/',
    >
    > so you'll need to maybe do a try/except around the open() call to open a
    >
    > Windows path like open("c:\\users\\io\\Documents\\markets.csv") or
    >
    > whatever. OR you'll need to do some OS detection ahead of time to set
    >
    > the file path properly.


    Well, I'll suggest an old trick I did long time ago to resolve
    the platform dependent problem in python.

    Just write a generator as a buffered input method to work under some
    file of some OS correctly first, then you can pass the generator which
    is tested rigorously to act properly in the function or the object which will
    use the generator to sip up the data to perform jobs.
    88888 Dihedral, Mar 5, 2013
    #5
  6. On Tuesday, March 5, 2013 3:20:28 AM UTC+8, ian douglas wrote:
    > On 03/04/2013 11:06 AM, io wrote:
    >
    > > esclusioni_file = open('/home/io/btc_trading/exclusions.txt','r')

    >
    > >

    >
    > > Windows error :

    >
    > >

    >
    > > Traceback (most recent call last):

    >
    > > File "C:\btc_trading\scripts

    >
    > > \import_json_2_csv_from_web_and_exclusions.py", line 10, in <module>

    >
    > > f = open("/home/io/btc_trading/markets.csv","wb")

    >
    > > IOError: [Errno 2] No such file or directory: '/home/io/btc_trading/

    >
    > > markets.csv'

    >
    >
    >
    > The error tells you everything you need to know: the file system has no
    >
    > path/file called "/home/io/btc_trading/markets.csv"
    >
    >
    >
    > I imagine your CSV file lives in a different location on your Windows 7
    >
    > system (which also uses back-slashes '\' instead of forward-slashes '/',
    >
    > so you'll need to maybe do a try/except around the open() call to open a
    >
    > Windows path like open("c:\\users\\io\\Documents\\markets.csv") or
    >
    > whatever. OR you'll need to do some OS detection ahead of time to set
    >
    > the file path properly.


    Well, I'll suggest an old trick I did long time ago to resolve
    the platform dependent problem in python.

    Just write a generator as a buffered input method to work under some
    file of some OS correctly first, then you can pass the generator which
    is tested rigorously to act properly in the function or the object which will
    use the generator to sip up the data to perform jobs.
    88888 Dihedral, Mar 5, 2013
    #6
    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. murali
    Replies:
    1
    Views:
    898
    Phlip
    Jun 25, 2003
  2. Mufasa
    Replies:
    2
    Views:
    339
    Michael Nemtsev
    Apr 19, 2007
  3. Fox
    Replies:
    5
    Views:
    107
  4. Wolfgang Strobl
    Replies:
    0
    Views:
    137
    Wolfgang Strobl
    Mar 4, 2013
  5. Terry Reedy
    Replies:
    0
    Views:
    150
    Terry Reedy
    Mar 4, 2013
Loading...

Share This Page