editing batch filenames

Wenn du dir nicht sicher bist, in welchem der anderen Foren du die Frage stellen sollst, dann bist du hier im Forum für allgemeine Fragen sicher richtig.
Antworten
drpython
User
Beiträge: 4
Registriert: Freitag 23. Juni 2006, 21:29

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
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
drpython
User
Beiträge: 4
Registriert: Freitag 23. Juni 2006, 21:29

danke!

can I replace

Code: Alles auswählen

for filename in filenames
with

Code: Alles auswählen

for filename in path
?
Benutzeravatar
gerold
Python-Forum Veteran
Beiträge: 5555
Registriert: Samstag 28. Februar 2004, 22:04
Wohnort: Oberhofen im Inntal (Tirol)
Kontaktdaten:

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
:-)
Zuletzt geändert von gerold am Samstag 24. Juni 2006, 00:01, insgesamt 1-mal geändert.
http://halvar.at | Kleiner Bascom AVR Kurs
Wissen hat eine wunderbare Eigenschaft: Es verdoppelt sich, wenn man es teilt.
drpython
User
Beiträge: 4
Registriert: Freitag 23. Juni 2006, 21:29

is filename a variable? forgive me, I'm VERY new to python.
Python 47
User
Beiträge: 574
Registriert: Samstag 17. September 2005, 21:04

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:
mfg

Thomas :-)
drpython
User
Beiträge: 4
Registriert: Freitag 23. Juni 2006, 21:29

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?
Antworten