Move the read files along with same named different format files

此生再无相见时 提交于 2019-12-13 04:17:01

问题


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 is a .dat file with exact same name for each .fits file (for example kkk.fits , kkk_trl.dat ) and I want the same named .dat file to move along with the .fits file together to the new folder.

 import os
 import glob
 import pyfits
 import shutil


for fitsName in glob.glob('*.fits'):
hdulist = pyfits.open(fitsName)
hdu = hdulist[0]
a = hdulist[0].header['OBJECT']
if a == "Bach":
shutil.move(fitsName, '/home/Bach/')
   b == a + '.dat'
    shutil.move(b, '/home/Bach/')

With this code I am able to move all the .fits files with ease but .dat files remains.

The code below is close to the answer but gives another problem that this time the code tries to move a file named kkk.fits.dat with the last line. I need it to read the file as kkk_trl.dat

import os
import glob
import pyfits
import shutil


for fitsName in glob.glob('*.fits'):
hdulist = pyfits.open(fitsName)
hdu = hdulist[0]
a = hdulist[0].header['OBJECT']
if a == "Bach":
shutil.move(fitsName, '/home/Bach/')
shutil.move((fitsName +'*.dat') , '/home/Bach/')

Latest step taken towards to the solution is below. This time code gives no error but only the .fits file goes to the destination folder and .dat file stays there.

import os
import glob
import pyfits
import shutil


for fitsName in glob.glob('*.fits'):
hdulist = pyfits.open(fitsName)
hdu = hdulist[0]
a = hdulist[0].header['OBJECT']
if a == "Bach":
shutil.move(fitsName, '/home/Bach/')
b = os.path.splitext(fitsName[0]) + '_trl.dat'
shutil.move(b, '/home/Bach/')

回答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?




回答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.



来源:https://stackoverflow.com/questions/55476516/move-the-read-files-along-with-same-named-different-format-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!