0

关于重命名文件夹中的文件名的问题。我的文件名如下所示:

EPG CRO 24 Kitchen 09.2013.xsl

在名称空间之间,我使用了这样的代码:

#!/usr/bin/python
# -*- coding: utf-8 -*-

# Remove whitespace from files where EPG named with space " " replace with "_"
for filename in os.listdir("."):
    if filename.find("2013|09 ") > 0:
        newfilename = filename.replace(" ","_")
        os.rename(filename, newfilename)

使用此代码,我删除了空格,但是如何从文件名中删除日期,使其看起来像这样:EPG_CRO_24_Kitche.xsl. 你能给我一些解决方案吗?

4

4 回答 4

1

小切片怎么样:

newfilename = input1[:input1.rfind(" ")].replace(" ","_")+input1[input1.rfind("."):]
于 2013-09-26T18:19:23.163 回答
1

如果所有文件名都具有相同的格式:NAME_20XX_XX.xsl,那么您可以使用 python 的列表slicing而不是regex

name.replace(' ','_')[:-12] + '.xsl'
于 2013-09-26T18:17:29.373 回答
1

如果日期的格式始终相同;

>>> s = "EPG CRO 24 Kitchen 09.2013.xsl"
>>> re.sub("\s+\d{2}\.\d{4}\..{3}$", "", s)
'EPG CRO 24 Kitchen'
于 2013-09-26T18:17:33.733 回答
1

正则表达式

正如 utdemir 所逃避的那样,正则表达式在这种情况下确实可以提供帮助。如果您从未接触过它们,一开始可能会感到困惑。查看https://www.debuggex.com/r/4RR6ZVrLC_nKYs8g以获取帮助您构建正则表达式的有用工具。

解决方案

更新的解决方案是:

import re

def rename_file(filename):
  if filename.startswith('EPG') and ' ' in filename:
    # \s+       means 1 or more whitespace characters                                   
    # [0-9]{2}  means exactly 2 characters of 0 through 9                               
    # \.        means find a '.' character                                              
    # [0-9]{4}  means exactly 4 characters of 0 through 9                               
    newfilename = re.sub("\s+[0-9]{2}\.[0-9]{4}", '', filename)
    newfilename = newfilename.replace(" ","_")
    os.rename(filename, newfilename)

边注

# Remove whitespace from files where EPG named with space " " replace with "_"
for filename in os.listdir("."):
    if filename.find("2013|09 ") > 0:
        newfilename = filename.replace(" ","_")
        os.rename(filename, newfilename)

除非我弄错了,否则您在上面所做的评论filename.find("2013|09 ") > 0将不起作用。

鉴于以下情况:

In [76]: filename = "EPG CRO 24 Kitchen 09.2013.xsl"
In [77]: filename.find("2013|09 ")
Out[77]: -1

而您所描述的评论,您可能想要更多类似的东西:

In [80]: if filename.startswith('EPG') and ' ' in filename:
   ....:     print('process this')
   ....:     
process this
于 2013-09-26T19:02:12.773 回答