File-writing not working in Windows?

Discussion in 'Python' started by tdahsu@gmail.com, Jun 6, 2008.

  1. Guest

    All,

    I have the following code:
    for fileTarget in dircache.listdir("directory"):
    (dirName, fileName) = os.path.split(fileTarget)
    f = open(fileTarget).readlines()
    copying = False
    for i in range(len(f)):
    for x in range (0,24,1):
    if (re.search(self.Info[x][3], f)):
    #If we have a match for our start of section...
    if (self.Info[x][2] == True):
    #And it's a section we care about...
    copying =
    True #Let's start copying the lines out
    to the temporary file...
    if (os.name == "posix"):
    if (self.checkbox25.GetValue() ==
    False):
    tempfileName = "tempdir/" +
    self.Info[x][0] + "_tmp_" + fileName + ".txt"
    else:
    tempfileName =
    self.textctrl07.GetValue() + "/" + self.Info[x][0] + "_xyz.txt"
    else:
    if (self.checkbox25.GetValue() ==
    False):
    tempfileName = "tempdir\\" +
    self.Info[x][0] + "_tmp_" + fileName + ".txt"
    else:
    tempfileName =
    self.textctrl07.GetValue() + "\\" + self.Info[x][0] + "_xyz.txt"
    else:
    copying = False
    if (re.search(self.Info[x][4], f)):
    #Now we've matched the end of our section...
    copying =
    False #So let's stop copying out to
    the temporary file...
    if (copying == True):
    g = open(tempfileName,
    'a') #Open that file in append mode...

    g.write(f) #Write the line...
    g.close()

    This code works PERFECTLY in Linux. Where I have a match in the file
    I'm processing, it gets cut out from the start of the match until the
    end of the match, and written to the temporary file in tempdir.

    It does not work in Windows. It does not create or write to the
    temporary file AT ALL. It creates the tempdir directory with no
    problem.

    Here's the kicker: it works perfectly in Windows if Windows is running
    in VMware on a Linux host! (I assume that that's because VMware is
    passing some call to the host.)

    Can anyone tell me what it is that I'm missing which would prevent the
    file from being created on Windows natively?

    I'm sorry I can't provide any more of the code, and I know that that
    will hamper your efforts in helping me, so I apologise up front.

    Assumptions:
    You can assume self.checkbox25.GetValue() is always false for now, and
    self.Info[x][0] contains a two character string like "00" or "09" or
    "14". There is always a match in the fileTarget, so self.Info[x][2]
    will always be true at some point, as will self.Info[x][4]. I am
    cutting an HTML file at predetermined comment sections, and I have
    control over the HTML files that are being cut. (So I can force the
    file to match what I need it to match if necessary.)

    I hope however that it's something obvious that a Python guru here
    will be able to spot and that this n00b is missing!

    Thanks!
     
    , Jun 6, 2008
    #1
    1. Advertising

  2. jay graves Guest

    On Jun 6, 10:18 am, wrote:
    <snippage>
    > This code works PERFECTLY in Linux. Where I have a match in the file
    > I'm processing, it gets cut out from the start of the match until the
    > end of the match, and written to the temporary file in tempdir.
    > It does not work in Windows. It does not create or write to the
    > temporary file AT ALL. It creates the tempdir directory with no
    > problem.


    In general, I don't use string concatenation when building paths.
    Especially on scripts that are meant to run on multiple platforms.


    > Here's the kicker: it works perfectly in Windows if Windows is running
    > in VMware on a Linux host! (I assume that that's because VMware is
    > passing some call to the host.)


    probably a red herring.

    > Can anyone tell me what it is that I'm missing which would prevent the
    > file from being created on Windows natively?


    Get rid of the 'posix' check and use os.path.join to create
    'tempfileName' and see if it works.

    HTH.
    ....
    Jay Graves
     
    jay graves, Jun 6, 2008
    #2
    1. Advertising

  3. Guest

    On Jun 6, 11:35 am, jay graves <> wrote:
    > On Jun 6, 10:18 am, wrote:
    > <snippage>
    >
    > > This code works PERFECTLY in Linux.  Where I have a match in the file
    > > I'm processing, it gets cut out from the start of the match until the
    > > end of the match, and written to the temporary file in tempdir.
    > > It does not work in Windows.  It does not create or write to the
    > > temporary file AT ALL.  It creates the tempdir directory with no
    > > problem.

    >
    > In general, I don't use string concatenation when building paths.
    > Especially on scripts that are meant to run on multiple platforms.
    >
    > > Here's the kicker: it works perfectly in Windows if Windows is running
    > > in VMware on a Linux host!  (I assume that that's because VMware is
    > > passing some call to the host.)

    >
    > probably a red herring.
    >
    > > Can anyone tell me what it is that I'm missing which would prevent the
    > > file from being created on Windows natively?

    >
    > Get rid of the 'posix' check and use os.path.join to create
    > 'tempfileName' and see if it works.
    >
    > HTH.
    > ...
    > Jay Graves


    Jay,

    Thank you for your answer. I have researched os.path.join. I have
    changed the code to read as follows:

    if (self.checkbox25.GetValue() == False):
    initialFileName = self.Info[x][0] + "_tmp_" + fileName + ".txt"
    tempfileName = os.path.join("proctemp", initialFileName)
    else:
    initialFileName = self.Info[x][0] + "_xyz.txt"
    tempfileName = os.path.join("proctemp", initialFileName)

    this still works in Linux; it does not work in Windows.

    I am thinking that the "g.open(tempFileName, 'a')" command is the
    issue. Is there anything different about opening a file in Windows?
    Does Windows understand "append", or would I have to do control checks
    for seeing if the file is created and then appending?
     
    , Jun 6, 2008
    #3
  4. jay graves Guest

    On Jun 6, 1:22 pm, wrote:
    > I am thinking that the "g.open(tempFileName, 'a')" command is the
    > issue. Is there anything different about opening a file in Windows?
    > Does Windows understand "append", or would I have to do control checks
    > for seeing if the file is created and then appending?


    Does your file have embedded nulls?
    Try opening it in binary mode.

    g.open(tempFileName,'ab')

    Note that this will turn off Universal newline support.

    Barring that, can you distill the problem down by writing a script
    that exhibits the behavior without the rest of your program?

    ....
    Jay
     
    jay graves, Jun 6, 2008
    #4
  5. Guest

    On Jun 6, 2:58 pm, jay graves <> wrote:
    > On Jun 6, 1:22 pm, wrote:
    >
    > > I am thinking that the "g.open(tempFileName, 'a')" command is the
    > > issue.  Is there anything different about opening a file in Windows?
    > > Does Windows understand "append", or would I have to do control checks
    > > for seeing if the file is created and then appending?

    >
    > Does your file have embedded nulls?
    > Try opening it in binary mode.
    >
    > g.open(tempFileName,'ab')
    >
    > Note that this will turn off Universal newline support.
    >
    > Barring that, can you distill the problem down by writing a script
    > that exhibits the behavior without the rest of your program?
    >
    > ...
    > Jay


    Jay,

    This did not make a difference in my script. However, I did what you
    suggested, and tried the simple script it Windows, and it works as it
    should.

    (It's really annoying because it works on the Mac and Linux! (I just
    tested my script on the Mac as well.) It only doesn't work on
    Windows, though clearly the file processing I am doing SHOULD work.)

    Now I have to find out what it is about my code that's causing the
    problem... :-(
     
    , Jun 6, 2008
    #5
  6. jay graves Guest

    On Jun 6, 3:19 pm, wrote:
    > This did not make a difference in my script. However, I did what you
    > suggested, and tried the simple script it Windows, and it works as it
    > should.
    > (It's really annoying because it works on the Mac and Linux! (I just
    > tested my script on the Mac as well.) It only doesn't work on
    > Windows, though clearly the file processing I am doing SHOULD work.)


    Is there a global exception handler somewhere in your code that could
    be eating an error that only happens on windows? (something weird
    like file permissions.) I'm really at a loss.

    Sorry.

    ....
    Jay
     
    jay graves, Jun 6, 2008
    #6
  7. John Machin Guest

    On Jun 7, 1:18 am, wrote:
    > All,
    >


    [code snipped]

    >
    > This code works PERFECTLY in Linux. Where I have a match in the file
    > I'm processing, it gets cut out from the start of the match until the
    > end of the match, and written to the temporary file in tempdir.
    >
    > It does not work in Windows. It does not create or write to the
    > temporary file AT ALL. It creates the tempdir directory with no
    > problem.
    >
    > Here's the kicker: it works perfectly in Windows if Windows is running
    > in VMware on a Linux host! (I assume that that's because VMware is
    > passing some call to the host.)
    >
    > Can anyone tell me what it is that I'm missing which would prevent the
    > file from being created on Windows natively?
    >
    > I'm sorry I can't provide any more of the code, and I know that that
    > will hamper your efforts in helping me, so I apologise up front.
    >
    > Assumptions:
    > You can assume self.checkbox25.GetValue() is always false for now, and
    > self.Info[x][0] contains a two character string like "00" or "09" or
    > "14". There is always a match in the fileTarget, so self.Info[x][2]
    > will always be true at some point, as will self.Info[x][4]. I am
    > cutting an HTML file at predetermined comment sections, and I have
    > control over the HTML files that are being cut. (So I can force the
    > file to match what I need it to match if necessary.)


    Assume nothing. Don't believe anyone who says "always". Insert some
    print statements and repr() calls to show what's actually there.

    > I hope however that it's something obvious that a Python guru here
    > will be able to spot and that this n00b is missing!


    *IF* the problem is in the code, it would be easier to spot if you had
    removed large chunks of indentation before posting.

    Less is more: change "if (foo == 2):" to "if foo == 2:", "foo == True"
    to "foo", and "foo == False" to "not foo".

    Browse http://www.python.org/dev/peps/pep-0008/

    HTH,
    John
     
    John Machin, Jun 7, 2008
    #7
  8. Ivan Guest

    Re: Re: File-writing not working in Windows?

    John Machin ?????:
    > On Jun 7, 1:18 am, wrote:
    >
    > Assume nothing. Don't believe anyone who says "always". Insert some
    > print statements and repr() calls to show what's actually there.
    >
    >
    >> I hope however that it's something obvious that a Python guru here
    >> will be able to spot and that this n00b is missing!
    >>

    >
    > *IF* the problem is in the code, it would be easier to spot if you had
    > removed large chunks of indentation before posting.
    >
    > Less is more: change "if (foo == 2):" to "if foo == 2:", "foo == True"
    > to "foo", and "foo == False" to "not foo".
    >
    > Browse http://www.python.org/dev/peps/pep-0008/
    >
    > HTH,
    > John
    >

    Hello,
    I tried os.path.join() under Windows XP and everything works as
    expected. The problem is in your script. You may use logger or use
    additional checks (John Machin wrote about this practice).

    TBRDs,
    Ivan
     
    Ivan, Jun 7, 2008
    #8
  9. Lie Guest

    On Jun 6, 10:18 pm, wrote:
    > All,
    >
    > I have the following code:
    >            for fileTarget in dircache.listdir("directory"):
    >                 (dirName, fileName) = os.path.split(fileTarget)
    >                 f = open(fileTarget).readlines()
    >                 copying = False
    >                 for i in range(len(f)):
    >                     for x in range (0,24,1):
    >                         if (re.search(self.Info[x][3], f)):
    > #If we have a match for our start of section...
    >                             if (self.Info[x][2] == True):
    > #And it's a section we care about...
    >                                 copying =
    > True                              #Let's start copying the lines out
    > to the temporary file...
    >                                 if (os.name == "posix"):
    >                                     if (self.checkbox25.GetValue() ==
    > False):
    >                                         tempfileName = "tempdir/" +
    > self.Info[x][0] + "_tmp_" + fileName + ".txt"
    >                                     else:
    >                                         tempfileName =
    > self.textctrl07.GetValue() + "/" + self.Info[x][0] + "_xyz.txt"
    >                                 else:
    >                                     if (self.checkbox25.GetValue() ==
    > False):
    >                                         tempfileName = "tempdir\\" +
    > self.Info[x][0] + "_tmp_" + fileName + ".txt"
    >                                     else:
    >                                         tempfileName =
    > self.textctrl07.GetValue() + "\\" + self.Info[x][0] + "_xyz.txt"
    >                             else:
    >                                 copying = False
    >                         if (re.search(self.Info[x][4], f)):
    > #Now we've matched the end of our section...
    >                             copying =
    > False                                 #So let's stop copying out to
    > the temporary file...
    >                     if (copying == True):
    >                         g = open(tempfileName,
    > 'a')                     #Open that file in append mode...
    >
    > g.write(f)                                       #Write the line...
    >                         g.close()
    >
    > This code works PERFECTLY in Linux.  Where I have a match in the file
    > I'm processing, it gets cut out from the start of the match until the
    > end of the match, and written to the temporary file in tempdir.
    >
    > It does not work in Windows.  It does not create or write to the
    > temporary file AT ALL.  It creates the tempdir directory with no
    > problem.
    >
    > Here's the kicker: it works perfectly in Windows if Windows is running
    > in VMware on a Linux host!  (I assume that that's because VMware is
    > passing some call to the host.)
    >
    > Can anyone tell me what it is that I'm missing which would prevent the
    > file from being created on Windows natively?
    >
    > I'm sorry I can't provide any more of the code, and I know that that
    > will hamper your efforts in helping me, so I apologise up front.
    >
    > Assumptions:
    > You can assume self.checkbox25.GetValue() is always false for now, and
    > self.Info[x][0] contains a two character string like "00" or "09" or
    > "14".  There is always a match in the fileTarget, so self.Info[x][2]
    > will always be true at some point, as will self.Info[x][4].  I am
    > cutting an HTML file at predetermined comment sections, and I have
    > control over the HTML files that are being cut.  (So I can force the
    > file to match what I need it to match if necessary.)
    >
    > I hope however that it's something obvious that a Python guru here
    > will be able to spot and that this n00b is missing!
    >
    > Thanks!


    Well, not to be rude, but that's quite a spaghetti code, some of the
    guilt, however, was for the mailing program that cuts 80+ lines.
    Others was the use of things like "for i in range(len(f)):" or "if (a
    == True)".

    Try checking whether you're trying to write to a path like r"\dir
    \file.txt" or r"dir\file.txt" instead of r"C:\dir\file.txt" in
    Windows.

    If that doesn't solve the problem, tell us a few things:
    - Any error messages? Or simply nothing is written out?
    - Has a blank file get created?
     
    Lie, Jun 8, 2008
    #9
  10. Guest

    On Jun 8, 4:11 am, Lie <> wrote:
    > On Jun 6, 10:18 pm, wrote:
    >
    >
    >
    >
    >
    > > All,

    >
    > > I have the following code:
    > >            for fileTarget in dircache.listdir("directory"):
    > >                 (dirName, fileName) = os.path.split(fileTarget)
    > >                 f = open(fileTarget).readlines()
    > >                 copying = False
    > >                 for i in range(len(f)):
    > >                     for x in range (0,24,1):
    > >                         if (re.search(self.Info[x][3], f)):
    > > #If we have a match for our start of section...
    > >                             if (self.Info[x][2] == True):
    > > #And it's a section we care about...
    > >                                 copying =
    > > True                              #Let's start copying the lines out
    > > to the temporary file...
    > >                                 if (os.name == "posix"):
    > >                                     if (self.checkbox25.GetValue() ==
    > > False):
    > >                                         tempfileName = "tempdir/" +
    > > self.Info[x][0] + "_tmp_" + fileName + ".txt"
    > >                                     else:
    > >                                         tempfileName =
    > > self.textctrl07.GetValue() + "/" + self.Info[x][0] + "_xyz.txt"
    > >                                 else:
    > >                                     if (self.checkbox25.GetValue() ==
    > > False):
    > >                                         tempfileName = "tempdir\\" +
    > > self.Info[x][0] + "_tmp_" + fileName + ".txt"
    > >                                     else:
    > >                                         tempfileName =
    > > self.textctrl07.GetValue() + "\\" + self.Info[x][0] + "_xyz.txt"
    > >                             else:
    > >                                 copying = False
    > >                         if (re.search(self.Info[x][4], f)):
    > > #Now we've matched the end of our section...
    > >                             copying =
    > > False                                 #So let's stop copying out to
    > > the temporary file...
    > >                     if (copying == True):
    > >                         g = open(tempfileName,
    > > 'a')                     #Open that file in append mode...

    >
    > > g.write(f)                                       #Write the line...
    > >                         g.close()

    >
    > > This code works PERFECTLY in Linux.  Where I have a match in the file
    > > I'm processing, it gets cut out from the start of the match until the
    > > end of the match, and written to the temporary file in tempdir.

    >
    > > It does not work in Windows.  It does not create or write to the
    > > temporary file AT ALL.  It creates the tempdir directory with no
    > > problem.

    >
    > > Here's the kicker: it works perfectly in Windows if Windows is running
    > > in VMware on a Linux host!  (I assume that that's because VMware is
    > > passing some call to the host.)

    >
    > > Can anyone tell me what it is that I'm missing which would prevent the
    > > file from being created on Windows natively?

    >
    > > I'm sorry I can't provide any more of the code, and I know that that
    > > will hamper your efforts in helping me, so I apologise up front.

    >
    > > Assumptions:
    > > You can assume self.checkbox25.GetValue() is always false for now, and
    > > self.Info[x][0] contains a two character string like "00" or "09" or
    > > "14".  There is always a match in the fileTarget, so self.Info[x][2]
    > > will always be true at some point, as will self.Info[x][4].  I am
    > > cutting an HTML file at predetermined comment sections, and I have
    > > control over the HTML files that are being cut.  (So I can force the
    > > file to match what I need it to match if necessary.)

    >
    > > I hope however that it's something obvious that a Python guru here
    > > will be able to spot and that this n00b is missing!

    >
    > > Thanks!

    >
    > Well, not to be rude, but that's quite a spaghetti code, some of the
    > guilt, however, was for the mailing program that cuts 80+ lines.
    > Others was the use of things like "for i in range(len(f)):" or "if (a
    > == True)".
    >
    > Try checking whether you're trying to write to a path like r"\dir
    > \file.txt" or r"dir\file.txt" instead of r"C:\dir\file.txt" in
    > Windows.
    >
    > If that doesn't solve the problem, tell us a few things:
    > - Any error messages? Or simply nothing is written out?
    > - Has a blank file get created?- Hide quoted text -
    >
    > - Show quoted text -


    Thanks to everyone for the help. I really do appreciate your
    suggestions and time; again I apologise for not being able to give you
    all the code to diagnose. I understand that it made this more
    difficult and thank you for the input!

    The error was not with the script.

    The error was with the HTML that was being parsed! It's the last
    thing I considered to check, and where I should have started.

    John, thank you for the link to the style guide. I can see its use
    and will make my code conform to the standards.

    Thanks again to everyone!
     
    , Jun 8, 2008
    #10
    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. darrel
    Replies:
    4
    Views:
    2,341
    Guadala Harry
    Nov 4, 2004
  2. HNguyen
    Replies:
    4
    Views:
    2,440
    HNguyen
    Dec 21, 2004
  3. Phoenix
    Replies:
    0
    Views:
    411
    Phoenix
    Jan 27, 2005
  4. Replies:
    1
    Views:
    170
    Shashi Shekhar
    Jul 17, 2006
  5. Jess
    Replies:
    1
    Views:
    135
Loading...

Share This Page