-3

I am brand new python user and looking for help. Need python scripts which does copy and version up (rename) the existed file. I tried to create scripts for version up, but it just adds 001 + 1 = 2 instead of v002. Also i am looking for first split the file name and then reassemble after doing math of version number. I am looking forward to someone who really can help on this. Does anyone can create and show me simple code which does above mentioned? Thanks in advance.

import os
import shutil

filename = 'New_Text_v001.txt'
fileparts = filename.split('.')

filesegments = fileparts[0].split('_')
fileVersion = filesegments[-1]

thirdSeg = fileVersion[1:5]

versionNum = int(thirdSeg) + 1

print filesegments, raw_input(versionNum), fileparts[1]
Mr. T
  • 11,960
  • 10
  • 32
  • 54
Khenrab
  • 1
  • 4
  • 2
    Do you have a question? "Need python scripts" is not a question. – user2357112 Jul 25 '18 at 23:42
  • 2
    If you have some code that's close to working but doesn't work, and you want help fixing it, instead of telling us a story about it, post the code (reduced to a [mcve]), tell us what it does wrong (by showing the desired vs. actual output), and tell us where you're stuck trying to fix it, and we can show you how to fix it. – abarnert Jul 25 '18 at 23:48

3 Answers3

0

you can use f strings

  1. for v in range(0,len(your_list):
  2. name_of_file = f'v{n:03}'

if you show me an example of the file name I could be able to help

  • file name is like "fish_modlUV_v001.ma". This filename has 4 segments. fish, modlUV, v001 and ext. I am wondering if script detects all these segments and get v001 and add 1 number to increment the version. So it will be "fish_modlUV_v002.ma" – Khenrab Jul 26 '18 at 00:34
0

This site is used to ask for help with problems that you have tried yourself. In future, I recommend adding your own code and showing us where you are stuck so people can help you with that problem rather having them do the entire problem for you. I will give you some snippets of code which will help you, but you should try to help yourself by searching on these forums before asking a question which can be resolved with some prior research.

String Manipulation

I recommend reading up on string concatenation and string splicing on:

https://docs.python.org/2/tutorial/introduction.html

File Renaming

How to rename a file with a specified directory:

import os
old_file = os.path.join("directory", "a.txt")
new_file = os.path.join("directory", "b.kml")
os.rename(old_file, new_file)

Now that you have the knowledge you need, you should attempt the problem yourself. If you have any further questions or if I missed anything, feel free to ask. The best way to learn is to do.

SamrajM
  • 620
  • 5
  • 12
0
import re

file_names = ["fish_modlUV_v002.ma","fish_modlUV_v001.ma","fish_modlUV_v103.ma","fish_modlUV_v041.ma"]
new_file_names = []
for s in file_names:
    digits = re.search('\d\d\d',s)
    start_num = digits.start()
    end_num = digits.end()
    strin_num= s[start_num:end_num]
    num = int(strin_num)
    num +=1
    new_file_names.append(s.replace(strin_num,f'{num:03}'))

hope this helps :)