我不明白你的代碼的邏輯。 我添加打印語句:
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('。','_ '))從不做任何事情。
順便說一句,我建議你用
for filename in os.listdir(folder):
if os.path.isfile(filename):
或更好的IMO更換
for root, dirs, filenames in os.walk(folder):
for filename in filenames:
(有一個壓痕以下)
for filename in (f in os.listdir(folder) if os.path.isfile(f)):
我覺得你採取的方式是一個僵局。如果我理解正確,事實上,你想要做的是在擴展名之前替換文件名中的點和'+',也就是說不用替換擴展名與擴展名之前的分隔符文件名,也不是擴展名中的'+'。無論如何,延期本身就有一個點和一個「+」是無稽之談。
所以你嘗試使用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
以及我剛纔sortof取得了劇本我已經和一些其他的代碼,我發現了一個frankenstine - 我想做的是將文件名從exstension分離出來,然後運行find和replace就可以了! – 2011-01-20 12:25:30
我現在就試試這個 - 謝謝! – 2011-01-20 12:42:05