在文件名中查找和替换

时间:2011-01-20 11:32:56

标签: python filenames replace

我正在运行一个脚本,如果文件名有'。',则抛出错误。或者在其中加一个'+',所以我试图制作一个替换所有'。'的脚本。使用'_',替换'+'可以正常工作。但更换'。'的问题如果我不拆分文件 - 然后我的所有文件都被删除!所以我试过拆分文件,但是现在它说脚本已经运行但是所有的'。'仍然存在!!

这是我的剧本:

folder = "C:/Documents and Settings/DuffA/Bureaublad/shortcuts projects/klic01/11G008689_1"

import glob, os
for filename in glob.glob(os.path.join(folder, "*+*")):
   os.rename(filename, filename.replace('+','_'))

for root, dirs, filenames in os.walk(folder): # returms root, dirs, and files
    for filename in filenames:
        filename_split = os.path.splitext(filename) # filename and extensionname (extension in [1])
        filename_zero = filename_split[0]
        extension = str.upper(filename_split[1])
        for filename_zero in glob.glob(os.path.join(filename_zero, "*.*")):
            os.rename(filename_zero, filename_zero.replace('.','_'))

提前谢谢!

2 个答案:

答案 0 :(得分:1)

为什么在glob for循环中使用os.walk并覆盖filename_zero变量?

for root, dirs, filenames in os.walk(folder): 
    for filename in filenames:
        filename_split = os.path.splitext(filename) # filename and extensionname (extension in [1])
        filename_zero = filename_split[0]
        extension = filename_split[1].upper()
        if "." in filename_zero:
            os.rename(filename_zero, filename_zero.replace('.','_'))

(未经测试)

答案 1 :(得分:1)

我不明白你的代码的逻辑。 我添加了打印声明:

folder = "C:/Documents and Settings/DuffA/Bureaublad/shortcuts projects/klic01/11G008689_1"

import glob, os
for filename in glob.glob(os.path.join(folder, "*+*")):
    print "I rename '+' to '_' in\n"+filename
    os.rename(filename, filename.replace('+','_'))


print '\n\n---- Now, there after, are the filenames in \n     '+folder


for root, dirs, filenames in os.walk(folder): # returms root, dirs, and files
    for filename in filenames:
        print '\nfilename==',filename
        filename_split = os.path.splitext(filename) # filename and extension name (extension in [1])
        filename_zero = filename_split[0]
        extension = str.upper(filename_split[1])
        print 'filename_zero==',filename_zero
        print 'os.path.join(filename_zero, "*.*")==',os.path.join(filename_zero, "*.*")
        print 'glob.glob(os.path.join(filename_zero, "*.*"))==',glob.glob(os.path.join(filename_zero, "*.*"))
        for filename_zero in glob.glob(os.path.join(filename_zero, "*.*")):
            print '  filename_zero in glob.glob(os.path.join(filename_zero, "*.*")) ==',filename_zero
            os.rename(filename_zero, filename_zero.replace('.','_'))

这是结果

I rename '+' to '_' in
C:/Documents and Settings/DuffA/Bureaublad/shortcuts projects/klic01/11G008689_1\+po.rt.hos.txt
I rename '+' to '_' in
C:/Documents and Settings/DuffA/Bureaublad/shortcuts projects/klic01/11G008689_1\ar.am+is.doc
I rename '+' to '_' in
C:/Documents and Settings/DuffA/Bureaublad/shortcuts projects/klic01/11G008689_1\ath+os.html
I rename '+' to '_' in
C:/Documents and Settings/DuffA/Bureaublad/shortcuts projects/klic01/11G008689_1\d'a.rtagn+an
I rename '+' to '_' in
C:/Documents and Settings/DuffA/Bureaublad/shortcuts projects/klic01/11G008689_1\dum+as.doc
I rename '+' to '_' in
C:/Documents and Settings/DuffA/Bureaublad/shortcuts projects/klic01/11G008689_1\ki.kiouili.do+c


---- Now, there after, are the filenames in 
     C:/Documents and Settings/DuffA/Bureaublad/shortcuts projects/klic01/11G008689_1

filename== ar.am_is.doc
filename_zero== ar.am_is
os.path.join(filename_zero, "*.*")== ar.am_is\*.*
glob.glob(os.path.join(filename_zero, "*.*"))== []

filename== arctic.txt
filename_zero== arctic
os.path.join(filename_zero, "*.*")== arctic\*.*
glob.glob(os.path.join(filename_zero, "*.*"))== []

filename== ath_os.html
filename_zero== ath_os
os.path.join(filename_zero, "*.*")== ath_os\*.*
glob.glob(os.path.join(filename_zero, "*.*"))== []

filename== atla.ntic.html
filename_zero== atla.ntic
os.path.join(filename_zero, "*.*")== atla.ntic\*.*
glob.glob(os.path.join(filename_zero, "*.*"))== []

filename== d'a.rtagn_an
filename_zero== d'a
os.path.join(filename_zero, "*.*")== d'a\*.*
glob.glob(os.path.join(filename_zero, "*.*"))== []

filename== dum_as.doc
filename_zero== dum_as
os.path.join(filename_zero, "*.*")== dum_as\*.*
glob.glob(os.path.join(filename_zero, "*.*"))== []

filename== ki.kiouili.do_c
filename_zero== ki.kiouili
os.path.join(filename_zero, "*.*")== ki.kiouili\*.*
glob.glob(os.path.join(filename_zero, "*.*"))== []

filename== _po.rt.hos.txt
filename_zero== _po.rt.hos
os.path.join(filename_zero, "*.*")== _po.rt.hos\*.*
glob.glob(os.path.join(filename_zero, "*.*"))== []

glob.glob(os.path.join(filename_zero,“”))总是[]因为 os.path.join(filename_zero, “”)是文件的名称而不是路径,然后指令 os.rename(filename_zero,filename_zero.replace('。','_'))< / strong>从不做任何事情。

顺便说一下,我建议你替换

for root, dirs, filenames in os.walk(folder):
    for filename in filenames:

for filename in os.listdir(folder):
    if os.path.isfile(filename):

或更好的imo(减少一个缩进)

for filename in ( f in os.listdir(folder) if os.path.isfile(f) ):

我认为你采取的方式是僵局。如果我理解正确,你想要做的事实上是在扩展名之前替换文件名中的点和'+',也就是说不替换在扩展名和扩展名之前的分隔符之间的分数。文件名,扩展名中也没有'+'。无论如何,扩展有一个点和一个'+'本身是无稽之谈。

所以你尝试使用glob。但个人,因为'。'的特殊情况。它界定了扩展,我不知道如何才能真正使用glob来实现这个目标。

所以我认为你必须采取另一种方式。 如果它们与通配符模式匹配并且仅返回必须处理的好文件名,则不必使glob检查所有文件名以验证,我们必须迭代文件名列表并尝试替换'+'和'。'。在扩展前部分。是的,会有一些文件名在这个地方没有积分和“+”,并且程序无关紧要。但无论如何,同样的工作是由glob在幕后进行的。因此,反对工作,我更喜欢编写我能想象的代码,也就是说没有glob。

以下代码在我看来是一个简短而有效的解决方案

folder = "C:/Documents and Settings/DuffA/Bureaublad/shortcuts projects/klic01/11G008689_1"

import os
separ = os.sep

for n in os.listdir(folder):
    print n
    if os.path.isfile(folder + separ + n):
        filename_zero, extension = os.path.splitext(n)
        os.rename(folder + separ + n , folder + separ + filename_zero.replace('.','_').replace('+','_') + extension)

print '\n--------------------------------\n'
for n in os.listdir(folder):
    print n

结果

+po.rt.hos.txt
ar.am+is.doc
arctic.txt
ath+os.html
atla.ntic.html
d'a.rtagn+an
dum+as.doc
ki.kiouili.do+c

--------------------------------

arctic.txt
ar_am_is.doc
ath_os.html
atla_ntic.html
d'a.rtagn+an
dum_as.doc
ki_kiouili.do+c
_po_rt_hos.txt