473,246 Members | 1,309 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,246 software developers and data experts.

File-writing not working in Windows?

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[i])):
#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[i])):
#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[i]) #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 27 '08 #1
9 1963
On Jun 6, 10:18 am, tda...@gmail.com 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
Jun 27 '08 #2
On Jun 6, 11:35*am, jay graves <jaywgra...@gmail.comwrote:
On Jun 6, 10:18 am, tda...@gmail.com 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 27 '08 #3
On Jun 6, 1:22 pm, tda...@gmail.com 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
Jun 27 '08 #4
On Jun 6, 2:58*pm, jay graves <jaywgra...@gmail.comwrote:
On Jun 6, 1:22 pm, tda...@gmail.com 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 27 '08 #5
On Jun 6, 3:19 pm, tda...@gmail.com 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


Jun 27 '08 #6
On Jun 7, 1:18 am, tda...@gmail.com 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
Jun 27 '08 #7

John Machin ?????:
On Jun 7, 1:18 am, tda...@gmail.com 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

Jun 27 '08 #8
Lie
On Jun 6, 10:18*pm, tda...@gmail.com 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[i])):
#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[i])):
#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[i]) * * * * * * * * * * * * * * * * * * * #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?
Jun 27 '08 #9
On Jun 8, 4:11*am, Lie <Lie.1...@gmail.comwrote:
On Jun 6, 10:18*pm, tda...@gmail.com 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[i])):
#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[i])):
#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[i]) * * * * * * * * * * * * * * * * * * * #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 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Brandon Walters | last post by:
I wrote a file download module for my website. The reason for the file download module is that my website downloads work on a credit based system. So I need to keep track of and limit daily...
5
by: Dave Smithz | last post by:
Hi There, I have a PHP script that sends an email with attachment and works great when provided the path to the file to send. However this file needs to be on the same server as the script. ...
3
by: Pernell Williams | last post by:
Hi all: I am new to Python, and this is my first post (and it won't be my last!), so HELLO EVERYONE!! I am attempting to use "xreadlines", an outer loop and an inner loop in conjunction with...
20
by: CHIN | last post by:
Hi all.. here s my problem ( maybe some of you saw me on other groups, but i cant find the solution !! ) I have to upload a file to an external site, so, i made a .vbs file , that logins to...
5
by: Pete | last post by:
I having a problem reading all characters from a file. What I'm trying to do is open a file with "for now" a 32bit hex value 0x8FB4902F which I want to && with a mask 0xFF000000 then >> right...
8
by: Sam Collett | last post by:
Is there a basic guide on Xml document creation and editing (simpler than the MSDN docs). Say I want to create a file containing the following: <?xml version="1.0" encoding="utf-8"...
0
by: troutbum | last post by:
I am experiencing problems when one user has a document open through a share pointing to the web site. I use the dsolefile to read the contents of a particular directory and then display them in a...
24
by: Kelly | last post by:
Hey all - I need a little more help. I don't quite know why my text file or form isn't closing. Short version - this program takes data entered into a textbox, user clicks Save button, Save...
2
by: mark | last post by:
How do I detect that a particular form element is a file upload or if the file upload has worked? In the Python cgi module documentation I found suggested code... form = cgi.FieldStorage()...
0
by: thjwong | last post by:
I'm using WinXP with Microsoft Visual C++ .NET 69462-006-3405781-18776, Microsoft Development Environment 2003 Version 7.1.3088, Microsoft .NET Framework 1.1 Version 1.1.4322 SP1 Most developers...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.