将读取的文件与相同命名的不同格式文件一起移动 [英] Move the read files along with same named different format files

查看:97
本文介绍了将读取的文件与相同命名的不同格式文件一起移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含很多.fits和.dat文件的文件.首先,我希望它们被一个参数分隔并移到另一个文件中(我已经完成了这部分).然后,问题是每个.fits文件都有一个名称完全相同的.dat文件(例如kkk.fits,kkk_trl.dat),我希望将同一个命名的.dat文件与.fits文件一起移动到新文件夹.

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/')

使用此代码,我可以轻松移动所有.fits文件,但仍保留.dat文件.

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

下面的代码很接近答案,但是又带来了另一个问题,这一次代码试图将最后一行的文件kkk.fits.dat移动.我需要它来读取文件为kkk_trl.dat

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/')

下面是对该解决方案采取的最新措施.此时间代码没有错误,但只有.fits文件进入目标文件夹,而.dat文件停留在该位置.

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/')

推荐答案

在此行

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

变量fitsName是类似于something.fits的字符串.现在,将字符串'*.dat'附加到它后面以按字面意义创建字符串something.fits*.dat.这意味着将文件移动为字面名为something.fits*.dat的文件,该文件可能不存在.您可能想要类似os.path.splitext(fitsName[0]) + '.dat'的东西.

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'.

还请注意,通配符扩展(例如*)通常对于接受文件名的Python函数没有意义.通配符扩展是 shell 的功能(例如,命令行).实际上,这就是您必须使用glob模块在Python中获得类似功能的原因. glob.glob('*.foo')与在shell中执行ls *.foo相似,但是您必须直接使用glob模块来实现该功能.通常,如果您将包含*的文件名传递给函数(例如shutil.move,尽管位于"shell utils"模块中,但却不支持通配符扩展),它将被视为文字*.

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 *.

另请参阅 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆