I want to change filename for all my files in a folder. They all end with a date and time like "filename 2019-05-20 1357" and I want the date first for all files. How can I do that simplest way?
Asked
Active
Viewed 273 times
0
-
1Possible duplicate of [Rename multiple files in a directory in Python](https://stackoverflow.com/questions/2759067/rename-multiple-files-in-a-directory-in-python) – Arkistarvh Kltzuonstev May 20 '19 at 12:02
-
But I dont want to delete first part of the name, I just want to change the order som I get the date first and then the name. – Catz May 20 '19 at 12:08
-
You can do something like this ```f='filename 2019-05-20 1357'``` and then ```f[9:] + f[:9]``` which will result in ```'2019-05-20 1357filename '```. – accdias May 20 '19 at 12:11
-
example output for the file name will be beneficial – sahasrara62 May 20 '19 at 12:11
3 Answers
2
#!/usr/bin/python3
import shutil, os, re
r = re.compile(r"^(.*) (\d{4}-\d{2}-\d{2} \d{4})$")
for f in os.listdir():
m = r.match(f)
if m:
shutil.move(f, "{} {}".format(m.group(2), m.group(1)))
Quick and roughly tested version

philipp
- 51
- 2
-
-
Easiest way is to run the script inside the directory where you want to rename the files. Or even put a os.chdir("/wherever/you/want/to/go") above the for loop. – philipp May 22 '19 at 09:03
0
import os
import re
import shutil
dir_path = '' # give the dir name
comp = re.compile(r'\d{4}-\d{2}-\d{2}')
for file in os.listdir(dir_path):
if '.' in file:
index = [i for i, v in enumerate(file,0) if v=='.'][-1]
name = file[:index]
ext = file[index+1:]
else:
ext=''
name = file
data = comp.findall(name)
if len(data)!=0:
date= comp.findall(name)[0]
rest_name = ' '.join(comp.split(name)).strip()
new_name = '{} {}{}'.format(date,rest_name,'.'+ext)
print('changing {} to {}'.format(name, new_name))
shutil.move(os.path.join(dir_path,name), os.path.join(dir_path, new_name))
else:
print('file {} is not change'.format(name))

sahasrara62
- 10,069
- 3
- 29
- 44
-
I get error line 6 "too many values to unpack (expected 2)", what could be wrong? – Catz May 21 '19 at 06:27
-
at what line you got this error, can you please write final output file name. plus are there other files in the directory with different file names – sahasrara62 May 21 '19 at 06:31
-
I get it on line 7 now, one file is for example called "Moving & Storage & [test] 2016-05-01" and now it expects 3 values. Another file is just called "Moving & Storage 2011-01-01" – Catz May 21 '19 at 06:39
-
It still complains about name, ext = file.split('.'), it expects 2 values to unpack. Is there a way to just move the last 10 digits of the filename to the beginning of the name no matter how long the filename is? – Catz May 21 '19 at 07:06
-
-
@Catz `name, ext = file.split('.'),` i thought there is extension of file , if not then just do `name=file` `"Error list index is out of range" I` i think this is because you have a file which didn't have date in it – sahasrara62 May 21 '19 at 07:59
-
Sorry to bother you again, but now I get "ValueError: too many values to unpack (expected 2)" for "name, ext = file.split('.')" – Catz May 21 '19 at 08:27
-
@Catz on of your file has a lot `.` present in name so thta's why this error. sol updated – sahasrara62 May 21 '19 at 08:48
-
Oh thats why. It says NameError name x is not defined for "index = [i for i, v in enumerate(x,0) if v=='.'][-1]" – Catz May 21 '19 at 08:55
-
Oh no, the error FileNotFoundError: [WinError 2] can't find file pops up. I entered only my dir like this: "dir_path = '/Users/Admin/Desktop/testfiles' ". Why does it believe I want to open a file? – Catz May 21 '19 at 10:06
-
adding linux path in window system is wrong, add the path from disk like `r"C:\Users\Admin\Desktop\testfiles"` – sahasrara62 May 21 '19 at 10:13
-
When I do that I get "FileNotFoundError: [WinError 3] Can't find search path: 'C:\\Users\\Admin\\Desktop\\testfiles'. Why two slashes? I entered only one like you said. – Catz May 21 '19 at 10:30
-
It seems like it is this line causing the FileNotFound error "os.rename(os.path.join(dir_path,name), os.path.join(dir_path, new_name))" – Catz May 21 '19 at 11:16
-
-
0
Here is my Implementation:
from datetime import datetime
import os
path = '/Users/name/desktop/directory'
for _, file in enumerate(os.listdir(path)):
os.rename(os.path.join(path, file), os.path.join(path, str(datetime.now().strftime("%d-%m-%Y %H%M"))+str(file)))
Output Format:
20-05-2019 1749filename.ext

Anubhav Singh
- 8,321
- 4
- 25
- 43