Python Forum
renaming the file in a suitable format, I just wondering if it is possible
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
renaming the file in a suitable format, I just wondering if it is possible
#1
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
Reply
#2
If I want to remove something from string I do something like that:

>>> s = 'spam&ham'
>>> s.replace('&', '')
'spamham'
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
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’
Reply
#4
(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.
Reply
#5
Thanks, i will take a look at that post. and will try to write it.
Reply
#6
(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
Reply
#7
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.
Reply
#8
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!!
Reply
#9
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)
Reply
#10
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().
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Information automatic document renaming lisa_d 2 337 Mar-20-2024, 06:34 PM
Last Post: Pedroski55
  renaming the 0 column in a dataframe Led_Zeppelin 5 1,510 Aug-16-2022, 04:07 PM
Last Post: deanhystad
  Functions to consider for file renaming and moving around directories cubangt 2 1,744 Jan-07-2022, 02:16 PM
Last Post: cubangt
  Help for the shortest way to install a suitable version of Python, Numpy, and OpenCV. Ezzat 2 2,275 Dec-23-2021, 12:34 PM
Last Post: snippsat
  Reshape txt file into particular format using python shantanu97 0 1,423 Dec-10-2021, 11:44 AM
Last Post: shantanu97
  How can we transcode encoding file uml url format Anldra12 9 3,377 Jul-25-2021, 09:30 AM
Last Post: Anldra12
  How to design a save file format? philipbergwerf 5 4,126 Apr-26-2021, 07:39 PM
Last Post: Gribouillis
  Python with win32com and EXIF renaming files. Amrcodes 4 3,656 Apr-03-2021, 08:51 PM
Last Post: DeaD_EyE
  suitable libraries for project dogbural 0 1,437 Dec-29-2020, 12:38 PM
Last Post: dogbural
  CPC File Format (Cartesian Perceptual Compression) - Can Python Convert / Handle Them PSKrieger 2 2,455 Nov-11-2020, 02:57 PM
Last Post: PSKrieger

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020