Seite 1 von 1

editing batch filenames

Verfasst: Freitag 23. Juni 2006, 21:33
von drpython
please forgive my lack of Deutsche, but I don't know where else to ask for help.

I have several files with file names that look like this:

mas.4.res.120
mas.4.res.124
mas.4.res.122
mas.4.res.128
mas.4.res.129

I need to extract the last three numbers and print it to a file.

Please help me with either python or awk

thanks again

Verfasst: Freitag 23. Juni 2006, 22:26
von BlackJack
Just split the names at the dots and use the last element:

Code: Alles auswählen

In [14]:filenames = ['mas.4.res.120', 'mas.4.res.124', 'mas.4.res.122']

In [15]:for filename in filenames:
   .15.:  print filename.split('.')[-1]
   .15.:
120
124
122

Verfasst: Freitag 23. Juni 2006, 22:28
von drpython
danke!

can I replace

Code: Alles auswählen

for filename in filenames
with

Code: Alles auswählen

for filename in path
?

Verfasst: Freitag 23. Juni 2006, 22:43
von gerold
drpython hat geschrieben:can I replace

Code: Alles auswählen

for filename in filenames
with

Code: Alles auswählen

for filename in path
Hi drpython!

I think, the simplest way is, to use "glob". Here´s an example:

Code: Alles auswählen

import glob

filelist = glob.glob("./mas*")
for filename in filelist:
    print filename.split(".")[-1]
Regards,
Gerold
:-)

Verfasst: Freitag 23. Juni 2006, 22:58
von drpython
is filename a variable? forgive me, I'm VERY new to python.

Verfasst: Freitag 23. Juni 2006, 23:57
von Python 47
drpython hat geschrieben:is filename a variable? forgive me, I'm VERY new to python.
Hi drpython

Yes it is a variable. How can i explain it to you? :?

ok a simple example:

Code: Alles auswählen

for x in (1, 2, 3, 4, 5):
   print x
So x becomes the value of each number. So in the first Run x=1, in the secound Run x=2 and so on. I hope you unterstand my little example.

But I think here
and here (7.3) it is besser explained. :wink:

Verfasst: Samstag 24. Juni 2006, 01:11
von drpython
no, that was a great explanation! thanks again!!!

I tried to run my Python code and there was something wrong...infinite loop I think...I must have typed something wrong...so I'll check tomorrow :)

thanks again !

Dankeshun...i think?