find and replace within a filename
I am running a script which throws and error if a filename has a '.' or a '+' in it, so i am trying to make a script which replaces all of the '.' with '_', replacing the '+' works fine. But the problem with replacing the '.' is that if i dont split the file - then all of my files get deleted!! So i have tried splitting the file, but now it says the script has run but all of the '.''s are still there!!
here is my script:
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('.','_'))
Thank you in advance!
I don't understand the logic of your code. I added print statements :
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('.','_'))
And here is the result
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, ".")) being always [] because os.path.join(filename_zero, ".") is a file's name and not a path, then the instruction os.rename(filename_zero, filename_zero.replace('.','_')) never do anything.
By the way, i suggest you to replace
for root, dirs, filenames in os.walk(folder):
for filename in filenames:
with
for filename in os.listdir(folder):
if os.path.isfile(filename):
or better imo(there one indentation less)
for filename in ( f in os.listdir(folder) if os.path.isfile(f) ):
I think the way you took is an impasse. If I understand correctly, what you want to do, in fact, is to replace points and '+' in a filename before the extension, that is to say not replacing the point that makes seperation between the extension and what's before the extension in a filename, nor a '+' in the extension. Anyway, it would be nonsense that an extension had a point and a '+' in itself.
So you try to use glob. But personnaly, because of the particular case of '.' which delimits the extension, I don't know how glob can really be employed for that goal.
So I think you must take another way. Instead of making glob to examine all the filenames to verifiy if they match with the wildcards pattern and to return only the good filenames that must be treated, we must iterate in the list of filenames and try to replace the '+' and '.' in the before-extension part. Yes, there will be filenames which won't have points and '+' at this place, and it will be vain work to do by the programm. But anyway, the same work is performed behind the scene by glob. So, work against work, I prefer to code the one that I can imagine, that's to say without glob.
The following code seems to me a short and efficent solution
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
Result
+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
why are you using glob
inside the os.walk
for loop and also overwriting the filename_zero variable?
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('.','_'))
(not tested)
精彩评论