No such file or directory error using xlwt to save a workbook to a network location.

590 views
Skip to first unread message

zbaby

unread,
Mar 7, 2014, 7:04:33 PM3/7/14
to python...@googlegroups.com
Hi,
 
I have a tried-and-true python script/program for converting txt files to xls. It is time to automate and all goes well until I try to save a converted .xls file to a network location other than the location I am running from.
 
Specs
Python 2.5.4
xlwt 0.7.4
Source:
Windows 7 Enterprise Build 7601 SP1
or
Windows Server 2008R2 Standard
Target:
Same machine, either PC or server- works
Different Server using UNC, either with server name or mapped drive (no shares)- fails
I have access to all Targets, as I am able to copy the input file to the same location, and am running as the same user. Also the program is finding the correct location, as it is calculating the correct Output file name.
I eliminated other factors, such as calling application and script, by calling the prog directly from the command line with full pathname in the parameter value.
I also tried '/' chars
The output file is very small, 30K.
 
Code Summary
#Output location and filename will be the same as input with xls extension
outputFile = ''.join(sys.argv[1])
#replace blanks with underscore in output file name
outputFile = outputFile.replace(" ", "_")
#replace .xls with .txt in output file name
outputFile = outputFile.replace(".txt", ".xls")
print "  Input file: ", sys.argv[1]
print " Output file: ", outputFile
book = xlwt.Workbook()
.
conversion
.
#Below is line 383
book.save(outputFile)
 
Works
Command: progname.py "C:\RootFolder\env\Filename.txt"
Output:
  Input file:  C:\RootFolder\env\Filename.txt
 Output file:  C:\RootFolder\env\Filename.xls
Note: Converted .xls file is available
 
Fails
Command: progname.py "\\serverName\RootFolder\env\Module\Filename.txt"
Output:
  Input file:  \\serverName\RootFolder\env\Module\Filename.txt
 Output file:  \\serverName\RootFolder\env\Module\Filename.xls
Note: the above displays from print statements are correct.
 
  File "C:\<loc of prog>\progname.py", line 383, in <module>
    book.save (outputFile)
  File "C:\Python25\lib\site-packages\xlwt\Workbook.py", line 643, in save
    doc.save(filename, self.get_biff_data())
  File "C:\Python25\lib\site-packages\xlwt\CompoundDoc.py", line 262, in save
    f = open(file_name_or_filelike_obj, 'w+b')
IOError: [Errno 2] No such file or directory: '\\\\serverName\\RootFolder\\env\\Module\\Filename.
xls'
 
OR
 
Command: progname.py "I:\RootFolder\env\Module\Filename.txt"
Output:
  Input file:  I:\RootFolder\env\Module\Filename.txt
 Output file:  I:\RootFolder\env\Module\Filename.xls
Note: displays are also correct here.
 
  File "C:\<loc of prog>\progname.py", line 383, in <module>
    book.save(outputFile)
  File "C:\Python25\lib\site-packages\xlwt\Workbook.py", line 643, in save
    doc.save(filename, self.get_biff_data())
  File "C:\Python25\lib\site-packages\xlwt\CompoundDoc.py", line 262, in save
    f = open(file_name_or_filelike_obj, 'w+b')
IOError: [Errno 2] No such file or directory: 'I:\\RootFolder\\env\\Module\\Filename.xls'
 
Thanks for this group!
 

John Machin

unread,
Mar 7, 2014, 9:38:44 PM3/7/14
to python...@googlegroups.com


On Saturday, March 8, 2014 11:04:33 AM UTC+11, zbaby wrote:
 
Code Summary
#Output location and filename will be the same as input with xls extension
unless input has one or more space characters ... but if you are running this from the command line, guff after the nth space will be in sys.argv[n+1]
 
outputFile = ''.join(sys.argv[1])

What is the point of ''.join(str_object) ? Did you mean <SPACE>.join(sys.argv[1<COLON>] ?
 
#replace blanks with underscore in output file name
outputFile = outputFile.replace(" ", "_")
#replace .xls with .txt in output file name
outputFile = outputFile.replace(".txt", ".xls")
print "  Input file: ", sys.argv[1]
print " Output file: ", outputFile

insert here:

f = open(outputFile, 'w+b') # If this fails, then xlwt is not part of the problem. Try Python 2.7.
 
book = xlwt.Workbook()
.

zbaby

unread,
Mar 10, 2014, 4:24:30 PM3/10/14
to python...@googlegroups.com
Thanks John, the insert worked.
 
About reading in the parameter value, that is the way it is standardly done where I work. I never got a good answer for why, but it seemed to work. I changed it to outputFile = sys.argv[1] and it also works.
 
About
Reply all
Reply to author
Forward
0 new messages