I’m having a problem with a small batch script that I’m writing. The point of the batch script is to run a Javascript file that converts an XML file to a csv file and then run a Python script which will analyze the just created csv file and create another csv file.
The batch script is below.
start XML-CSV_Converter.js
python CSV_ANALYZER.py
exit
I didn’t write the XML-CSV converter;
It can be found here. (http://gotochriswest.com/blog/2011/05/05/excel-batch-convert-xls-to-csv/) The only thing I changed was I removed all of the alerts and input prompts so it doesn’t wait on any user input. Simply put, all it does it look at every XML file in the current directory and produces a csv file in the same directory.
Whenever I run the batch script, I keep getting an IO error in my Python script because even though it can see the created file, it isn’t able to open the file.
The exact error is:
"IOError: [Errno 2] No such file or directory: 'NAME_OF_FILE.csv'"
The part of the Python script which is causing errors is listed below.
dirList = os.listdir("C:\FOLDER")
for fname in dirList:
if fname.find(".csv") != -1:
inputFile = open(fname,'r') <---- Script halts here
Anybody know what could be causing the file not being opened in the Python script?
If I run the JavaScript file manually, and then the Python script manually, it works perfectly. But when I try to chain them together in a batch file it breaks. I would appreciate any and all ideas!
Thanks in advance!
You forgot to add the folder name to the file name in the
open()call.listdir()returns only the file names, so unless its path argument matches the current directory, you’ll have to add it to the returned file names to properly identify them.As a side note, you should also avoid using backward slashes in normal strings. In this case “C:\FOLDER” happens to work, but “C:\folder” wouldn’t, because
\fwould be converted to\x0c(the linefeed character). You should use either a raw string (r'C:\FOLDER'which doesn’t convert escaped characters), an escaped backslash ('C:\\FOLDER'), or the more portable'C:/FOLDER'(on Windows'/'is the alternative path separator).Also a simpler way to check for the extension is to test whether the name ends with it (the
s.find(t) == -1is not very Pythonic). All in all, the modified code could look something like this: