2

How do i write a python script to split a file name

eg

LN0001_07272010_3.dat

and to rename the file to LN0001_JY_07272010?

also how do i place a '|' and the end of each line in this file(contents) each line is a record?

SilentGhost
  • 307,395
  • 66
  • 306
  • 293
user396123
  • 59
  • 1
  • 2
  • 12
  • 4
    These are two separate questions, and you should ask them separately. – Jesse Dhillon Jul 30 '10 at 17:34
  • The question is really about splitting a string, which the answers have covered. However, the title would lead me to things like [os.path.split()](http://docs.python.org/library/os.path.html#os.path.split), which operates differently. – GreenMatt Jul 30 '10 at 17:42
  • 2
    Probably the best answer to this question would be "read a introductory Python tutorial". Still, I posted an answer :-( – tokland Jul 30 '10 at 19:12

5 Answers5

6
fn = "LN0001_07272010_3.dat".split('_')
new_fn = '{0}_JY_{1}'.format(fn[0], fn[1])

Update forgot to add "JY" to new_fn

Jesse Dhillon
  • 7,841
  • 1
  • 34
  • 34
3
filename="LN0001_07272010_3.dat"
newfilename=filename.split("_")[0]+"_JY_"+filename.split("_")[1]

linearr=[]
for line in open(filename).readlines():
     linearr.append(line+"|")

f=open(newfilename, "w")
for line in linearr:
     f.write(line)
f.close()
amadain
  • 2,724
  • 4
  • 37
  • 58
2
name = "LN0001_07272010_3.dat"                    
parts = name.split('_')  # gives you ["LN0001","07272010","3.dat"]    
newname = parts[0] + "_STUFF_" + parts[1] ... etc

For rename you can either use the python filesystem function,

Or you can use python print to spit out a set of rename commands for your OS, capture that to a batch/shell file and AFTER checking that it looks correct - run it.

print "mv ",name,newname   # gives; mv LN0001_07272010_3.dat LN0001_JY_07272010
Martin Beckett
  • 94,801
  • 28
  • 188
  • 263
  • jesse's solution with the {0} is neater - but with a script it's often better to have simple and readable. – Martin Beckett Jul 31 '10 at 00:19
  • if you find it unreadable then you should familiarize yourself with that syntax: it's the preferred string formatting solution as of Python 3 http://www.python.org/dev/peps/pep-3101/ – Jesse Dhillon Jul 31 '10 at 17:17
  • @Jesse - I was just making the point that the most obvious, even if not the shortest, quickest or most elegent - is often the optimal solution for a script that is going to be run once. – Martin Beckett Jul 31 '10 at 22:53
  • sorry not trying to be a dick. I'm just saying that readability is to some extent a matter of taste, and as this is preferred way to format strings, one should acquire a taste for reading it. – Jesse Dhillon Jul 31 '10 at 23:54
0

Because in-place operations are usually a bad idea, here is a code that creates a new file leaving the original unchanged:

fn = "LN0001_07272010_3.dat"
fn1, fn2 = fn.split("_")[:2]
new_fn = "_".join([fn1, "JY", fn2])
open(new_fn, "w").writelines(line.rstrip()+"|\n" for line in open(fn))
tokland
  • 66,169
  • 13
  • 144
  • 170
0
file_name = 'LN0001_07272010_3.dat'
new_file_name = '{0}_JY_{1}'.format(file_name.split('_')[0], file_name.split('_')[1])