Skip to content Skip to sidebar Skip to footer

Move The Read Files Along With Same Named Different Format Files

I have a file that contains lots of .fits and .dat files. First, I want them separated by a parameter and moved to another file(I have this part done). Then, the problem is there i

Solution 1:

On this line

shutil.move((fitsName +'*.dat'), '/home/Bach/')`

the variable fitsName is a string looking like something.fits. Now you're appending to it the string '*.dat' to create the string something.fits*.dat literally. This means move the file the is literally named something.fits*.dat, which presumably does not exist. You probably want something like os.path.splitext(fitsName[0]) + '.dat'.

Note also that wildcard expansions like with * are not generally meaningful to functions in Python that accept filenames. Wildcard expansion is a feature of your shell (e.g. the command line). In fact that's the reason you have to use the glob module to get functionality like this in Python. glob.glob('*.foo') is similar to doing ls *.foo in your shell but you have to use the glob module directly for that functionality. In general if you pass a filename containing * to a function (e.g. like shutil.move, which despite being in the "shell utils" module does not support wildcard expansion) it will just treat is as a literal *.

Please see also my general tips for debugging simple Python programs at My python code that converts numbers between bases has several errors. What could be wrong and how can I find them?


Solution 2:

Assignment vs comparison

The statement b == a + '.dat' is equal to False and does not assign anything to b like b = a + '.dat' would.


Post a Comment for "Move The Read Files Along With Same Named Different Format Files"