Ey guys, I have a python script that merges a bunch of files together via the "python fileMerge.py <merged filename> <file(s)>" command in terminal/command-line. I would like to specify all files that have a a file extension of "log". I could do this in terminal with "python fileMerge mergedLog.txt *.log" but when I try doing that in Windows I get a complaint saying that "*.log" is not a file. If anyone could help me with this that would be great. If it were only a handful of files I wanted to specify I would have no problem just typing them out but there are a good 50 or so files I want to merge so that's why this is kind of essential to me. Thanks!
-
You don't need Python for a trivial job like this. Simply use the command prompt, and do something along the lines of "type *.log > all-log.txt".
-
Pirx is correct, and his solution is the best for your immediate problem (as I understand it)... but you may still want to understand why wildcards don't behave in Windows like they do in Unix. See, in Unix (and Linux), the shell expands the wildcards before giving it to the executable (or script). So when you type:
python fileMerge.py *.log
then the shell changes it to something like:
python fileMerge.py file1.log file2.log file3.log file4.log
...and then runs the command.
The Windows command shell doesn't do that. Windows just passes the wildcard straight through to the scriptm and the script gets *.log as its argument, and interprets that however it likes.
You can see this difference easily by running:
echo *.log
on both platforms. On Unix it lists all the files matching the wildcard in the current directory. In the Windows shell, it'll just output " *.log " literally.
One workaround is to install cygwin. This will let you run Unix shells such as bash inside Windows... so you'll get the Unix-style wildcard expansion. Another solution is to modify your python script to interpret and expand the wildcard... which is the way that built-in Windows shell commands work.
For some commands, it works to concatenate the files using the "type" command (similar to "cat" in Unix) before running the script... such as "type *.log | somescript" will feed the contents of all the log files smooshed together on standard input to the script... similarly to "cat *.log | somescript" on Unix. However, whether this will do what you need or not depends on the nature of the command. -
Very nice explanation, swarmer. While we are talking about simple solutions, I just did a quick search, and it seems there's lots of freeware utilities available that can also merge text files for you. Just bing "Windows file merge". For people who are afraid of the command line, those might be a good choice as well.
-
Ah thanks fellas. The "type *.log > all-log.txt" did the trick and yes Swarmer, I was going to ask why the command-line doesnt behave like the terminal, but you already answered that for me. Thanks again! I'll post back here if I have any further problems/questions.
Command-Line Woes
Discussion in 'Windows OS and Software' started by Stunner, Feb 1, 2010.