使用.group()重命名带有正则表达式的文件

时间:2022-10-26 10:33:22

I need to rename files which starts with numbers from a given directory, the files I have are like this:

我需要重命名以给定目录中的数字开头的文件,我拥有的文件是这样的:

"my_folder/1. file_one.csv"
"my_folder/2.file_two_1.csv"
"my_folder/file_three.csv"
"my_folder/file_four.csv"

I want the files with as this:

我想要这样的文件:

"my_folder/file_one.csv"
"my_folder/file_two_1.csv"
"my_folder/file_three.csv"
"my_folder/file_four.csv"

So I need to remove when it exists:

所以我需要删除它存在时:

  1. The digit at the begging
  2. 乞讨的数字

  3. The dot
  4. The space

My first try was to use following regex:

我的第一个尝试是使用以下正则表达式:

re.sub('\d.\s*','',name)

but it brakes the name of the file is cases like:

但它刹车文件的名称是这样的情况:

"2.file_two_1.csv" --> "file_two_csv"

“2.file_two_1.csv” - >“file_two_csv”

I got an error when I try to use .group()

我尝试使用.group()时出错

re.sub('/(\d.\s*)','',name).group(1)
"'unicode' object has no attribute 'group' "

Is it possible to use .group() for cases like that one?

是否可以使用.group()来处理那样的情况?

PD. I know I can solve the issue using this expression instead:

PD。我知道我可以使用这个表达式解决问题:

re.sub('/(\d.\s*)','/',name)

My question is regarding the use of the .group method or any similar approach.

我的问题是关于.group方法或任何类似方法的使用。

1 个解决方案

#1


2  

One approach using os would be:

使用os的一种方法是:

a = 'my_folder/1. file_one.csv'    
os.path.join(os.path.split(a)[0], re.sub('^\d\.\s*','',os.path.split(a)[1]))

Outputs: 'my_folder/file_one.csv'

^ will make sure the regular expression only happens at the start of the string.

^将确保正则表达式仅发生在字符串的开头。

Also notice the \. as you want to delete when a single dot is met. In regular expressions a single dot means any character.

还要注意\。如果要在满足单个点时删除。在正则表达式中,单个点表示任何字符。

#1


2  

One approach using os would be:

使用os的一种方法是:

a = 'my_folder/1. file_one.csv'    
os.path.join(os.path.split(a)[0], re.sub('^\d\.\s*','',os.path.split(a)[1]))

Outputs: 'my_folder/file_one.csv'

^ will make sure the regular expression only happens at the start of the string.

^将确保正则表达式仅发生在字符串的开头。

Also notice the \. as you want to delete when a single dot is met. In regular expressions a single dot means any character.

还要注意\。如果要在满足单个点时删除。在正则表达式中,单个点表示任何字符。