从第一个 space 之后的文件中删除文本
Strip text from files after first space
我正在尝试重命名大约几千个文件以仅包含它们的代码,文件的命名方式如下:
2834 The file
2312 The file
982 The file
期望的输出:
2834
2312
982
我想将它们重命名为的代码由 space 分隔,所以我只需要去掉 space 之后的文本。
我试过使用 os/glob/enumerate 只是按数字顺序重命名它们,这证明是有问题的,因为目录没有以相同的顺序返回,所以当我重命名它们时,代码混淆了。
您需要使用 glob
和 os
。一个简单的例子(带注释)如下:
import glob
import os
# iterate over all the files
for files in glob.glob('*.*'):
try:
new = files.replace("The file", '') # if there's a match replace names
os.rename(files, new) # rename the file
print files, new # just to make sure it's working
except:
print 'ERROR!!,', files # to see if there were any errors
或者,如果代码始终是前 4 个字符,您可以执行以下操作:
import glob
import os
# iterate over all the files
for files in glob.glob('*.*'):
try:
os.rename(files, files[0:4]) # rename the file
print files, new # just to make sure it's working
except:
print 'ERROR!!,', files # to see if there were any errors
刚注意到你的一个例子只有 3 个字符作为代码。更好的解决方案可能是在文件名上使用 .find(' ')
来定位准备好用于字符串切片的 space。例如:
import glob
import os
# iterate over all the files
for files in glob.glob('*.*'):
try:
os.rename(files, files[0: files.find(' ')]) # rename the file
print files # just to make sure it's working
except:
print 'ERROR!!,', files # to see if there were any errors
其他人已经演示了如何操作。所以我只建议更好的方法来获取字符串中的第一个单词:
filename = "12345 blahblahblah"
sfn = filename.split(" ", 1)
newfilename = sfn[0]
这样,如果字符串不包含“”,则不会发生任何事情,即相同的字符串将被 returned。
另一方面,如果未找到 " ",则使用 find() 将 return -1。而且,切片 filename[0:-1] 会去掉最后一个字符,这可能是不良影响。如果第一个字符是“”,两者都会产生一个空字符串。所以我提出更好的解决方案:
filename = " 12345 blahblahblah"
sfn = filename.split(None, 1)
newfilename = sfn[0]
如果需要除空格以外的其他分隔符,则应为:
filename = "____12345_blahblahblah"
sfn = [x for x in filename.split("_") if x!=""]
newfilename = sfn[0]
这对您来说就是完整的重命名。它保留扩展名并尊重完整路径。
import os
def RenameToFirstWord (filename):
filename = os.path.abspath(filename)
origfn = filename
path, filename = os.path.split(filename)
fn, ext = os.path.splitext(filename)
# If filename starts with extension separator (hidden files on *nixes):
if not fn: fn = ext; ext = ""
sfn = fn.split(None, 1)
newfn = sfn[0]+ext
try:
os.rename(origfn, os.path.join(path, newfn))
except Exception, e:
print "Cannot rename '%s' to '%s'!\nError is: '%s'\nand it is ignored!" % (filename, newfn, str(e))
使用glob.glob()
获取完整的文件列表(我建议给它一个完整的路径)。接下来仅过滤具有 .png
或 .jpg
扩展名的文件。接下来使用正则表达式提取所有数字。如果有多个组,则只取第一组数字。
最后,创建新文件名并使用os.rename()
重命名文件:
import glob
import os
import re
for filename in glob.glob(r'c:\my folder\*.*'):
path, name = os.path.split(filename)
extension = os.path.splitext(name)[1]
if extension.lower() in ['.jpg', '.png', '.jpeg']:
digits = re.findall('(\d+)', name)
if digits:
new_filename = os.path.join(path, '{}{}'.format(digits[0], extension))
print "{:30} {}".format(filename, new_filename) # show what is being renamed
os.rename(filename, new_filename)
例如:
2834 The file.jpg 2834.jpg
2312 The file.PNG 2312.PNG
982 The file.jpg 982.jpg
1234 test 4567.jpg 1234.jpg
The file 7133123.png 7133123.png
我正在尝试重命名大约几千个文件以仅包含它们的代码,文件的命名方式如下:
2834 The file
2312 The file
982 The file
期望的输出:
2834
2312
982
我想将它们重命名为的代码由 space 分隔,所以我只需要去掉 space 之后的文本。
我试过使用 os/glob/enumerate 只是按数字顺序重命名它们,这证明是有问题的,因为目录没有以相同的顺序返回,所以当我重命名它们时,代码混淆了。
您需要使用 glob
和 os
。一个简单的例子(带注释)如下:
import glob
import os
# iterate over all the files
for files in glob.glob('*.*'):
try:
new = files.replace("The file", '') # if there's a match replace names
os.rename(files, new) # rename the file
print files, new # just to make sure it's working
except:
print 'ERROR!!,', files # to see if there were any errors
或者,如果代码始终是前 4 个字符,您可以执行以下操作:
import glob
import os
# iterate over all the files
for files in glob.glob('*.*'):
try:
os.rename(files, files[0:4]) # rename the file
print files, new # just to make sure it's working
except:
print 'ERROR!!,', files # to see if there were any errors
刚注意到你的一个例子只有 3 个字符作为代码。更好的解决方案可能是在文件名上使用 .find(' ')
来定位准备好用于字符串切片的 space。例如:
import glob
import os
# iterate over all the files
for files in glob.glob('*.*'):
try:
os.rename(files, files[0: files.find(' ')]) # rename the file
print files # just to make sure it's working
except:
print 'ERROR!!,', files # to see if there were any errors
其他人已经演示了如何操作。所以我只建议更好的方法来获取字符串中的第一个单词:
filename = "12345 blahblahblah"
sfn = filename.split(" ", 1)
newfilename = sfn[0]
这样,如果字符串不包含“”,则不会发生任何事情,即相同的字符串将被 returned。 另一方面,如果未找到 " ",则使用 find() 将 return -1。而且,切片 filename[0:-1] 会去掉最后一个字符,这可能是不良影响。如果第一个字符是“”,两者都会产生一个空字符串。所以我提出更好的解决方案:
filename = " 12345 blahblahblah"
sfn = filename.split(None, 1)
newfilename = sfn[0]
如果需要除空格以外的其他分隔符,则应为:
filename = "____12345_blahblahblah"
sfn = [x for x in filename.split("_") if x!=""]
newfilename = sfn[0]
这对您来说就是完整的重命名。它保留扩展名并尊重完整路径。
import os
def RenameToFirstWord (filename):
filename = os.path.abspath(filename)
origfn = filename
path, filename = os.path.split(filename)
fn, ext = os.path.splitext(filename)
# If filename starts with extension separator (hidden files on *nixes):
if not fn: fn = ext; ext = ""
sfn = fn.split(None, 1)
newfn = sfn[0]+ext
try:
os.rename(origfn, os.path.join(path, newfn))
except Exception, e:
print "Cannot rename '%s' to '%s'!\nError is: '%s'\nand it is ignored!" % (filename, newfn, str(e))
使用glob.glob()
获取完整的文件列表(我建议给它一个完整的路径)。接下来仅过滤具有 .png
或 .jpg
扩展名的文件。接下来使用正则表达式提取所有数字。如果有多个组,则只取第一组数字。
最后,创建新文件名并使用os.rename()
重命名文件:
import glob
import os
import re
for filename in glob.glob(r'c:\my folder\*.*'):
path, name = os.path.split(filename)
extension = os.path.splitext(name)[1]
if extension.lower() in ['.jpg', '.png', '.jpeg']:
digits = re.findall('(\d+)', name)
if digits:
new_filename = os.path.join(path, '{}{}'.format(digits[0], extension))
print "{:30} {}".format(filename, new_filename) # show what is being renamed
os.rename(filename, new_filename)
例如:
2834 The file.jpg 2834.jpg
2312 The file.PNG 2312.PNG
982 The file.jpg 982.jpg
1234 test 4567.jpg 1234.jpg
The file 7133123.png 7133123.png