Python Forum

Full Version: renaming the file in a suitable format, I just wondering if it is possible
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I have a list of file which I have gathered them in time order, I should change the file name (just name) in order of the time.
The name changing process will contain;

Removing the name from ‘#’ sign until the end.
Save the name before ‘#’ and add the 1.1 for the first file and the numbering will continue until 10.2, I have a 20 file so at the end I will have below order:
Exist_name#1.1
Exist_name#1.2
Exist_name#2.1
Exist_name#2.2

Exist_name#10.1
Exist_name#10.2
If I want to remove something from string I do something like that:

>>> s = 'spam&ham'
>>> s.replace('&', '')
'spamham'
I did not say something about string. I have said that I have 20 files with different name which I have saved them in the time order. Which I mean the best way to numbering them would be the time.
For example name of the file:
Somename1#somename2

I am going to replace the ‘somename2’ in the file name with the number for example :‘1.1’
(Jun-11-2019, 09:39 AM)go127a Wrote: [ -> ]I did not say something about string. I have said that I have 20 files with different name which I have saved them in the time order.
Yes,but when working with filenames they are string and then can use eg replace().
(Jun-11-2019, 09:39 AM)go127a Wrote: [ -> ]Which I mean the best way to numbering them would be the time.
1.1 1.2 .. 10.1 10.2 is just a confusing way to do it.

Quick example.
files = '''\
somename111
somename2277
somename3555
somename4789'''.split('\n')
>>> import re 
>>> 
>>> for number,name in enumerate(files, 1):   
...     re.sub(r'\d+', f'_{number}', name)
...     
'somename_1'
'somename_2'
'somename_3'
'somename_4'
Can also look this post where i do renaming.
There use a other increment way with string formatting {index:03}.
Output:
001.png 002.png 003.png 004.png
So when come 10 it will 010.png.
Thanks, i will take a look at that post. and will try to write it.
(Jun-11-2019, 06:00 AM)go127a Wrote: [ -> ]I have a list of file which I have gathered them in time order, I should change the file name (just name) in order of the time.
The name changing process will contain;

Removing the name from ‘#’ sign until the end.
Save the name before ‘#’ and add the 1.1 for the first file and the numbering will continue until 10.2, I have a 20 file so at the end I will have below order:
Exist_name#01
Exist_name#02
....
Exist_name#19
Exist_name#20


Exist_name#10.1
Exist_name#10.2
It dos not help to just copy your first question.
You haven gotten hint/help to get you stated,getting finish solution with no effort is not the way it work here.
Write code and post what you struggle with.
I have tried to make my question more understandable here. a post before that I have mentioned that I am going to work on it!!
I have wrote below code but I dont know how can i put it in loop and update the second part of the new name for each file in the path.
import os
import glob
files = glob.glob('2019-06-13_#**.tws')
for file in files:
    parts = file.split('#') #[2019-06-13_, 01.tws]
    new_name = '1.1'.format(parts[2]) #2019-06-13_#1.1.tws
    os.rename(file, new_name)
When you insists to have that float range numbering,most write a code that dos that.
Can not just use range() as it don't take float.
With float also have to be careful so don't get filename like 2019-06-13_#0.30000000000000004.tws.
>>> 0.1 + 0.2
0.30000000000000004
import decimal

def float_range(start, stop, step):
  while start < stop:
    yield float(start)
    start += decimal.Decimal(step)

print(list(float_range(1, 11, '0.1')))
Output:
[1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0, 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 3.0, 3.1, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9, 4.0, 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 4.9, 5.0, 5.1, 5.2, 5.3, 5.4, 5.5, 5.6, 5.7, 5.8, 5.9, 6.0, 6.1, 6.2, 6.3, 6.4, 6.5, 6.6, 6.7, 6.8, 6.9, 7.0, 7.1, 7.2, 7.3, 7.4, 7.5, 7.6, 7.7, 7.8, 7.9, 8.0, 8.1, 8.2, 8.3, 8.4, 8.5, 8.6, 8.7, 8.8, 8.9, 9.0, 9.1, 9.2, 9.3, 9.4, 9.5, 9.6, 9.7, 9.8, 9.9, 10.0, 10.1, 10.2, 10.3, 10.4, 10.5, 10.6, 10.7, 10.8, 10.9]
Test:
import os
from glob import glob
import decimal

def float_range(start, stop, step):
  while start < stop:
    yield float(start)
    start += decimal.Decimal(step)

'''Before
2019-06-13_#11.tws
2019-06-13_#22.tws
2019-06-13_#333.tws
2019-06-13_#9955.tws
'''

for file,numb in zip(glob('*.tws'),float_range(1, 11, '0.1')):
    #print(file, numb)
    parts = file.partition('#')
    parts = ''.join(parts[0:2])
    print(f'{parts}{numb}.tws') # Test
Output:
2019-06-13_#1.0.tws 2019-06-13_#1.1.tws 2019-06-13_#1.2.tws 2019-06-13_#1.3.tws
So it's almost finish just missing os.rename().
Pages: 1 2