[Tutor] Linux variable to Python
Danny Yoo
dyoo at hkn.eecs.berkeley.edu
Fri May 13 23:00:50 CEST 2005
On Fri, 13 May 2005, Alberto Troiano wrote:
> Here is an example of what I want to do:
>
> I run over Linux shell the following command:
>
> [root at siseg root]# fec=cam`date +%Y%m%d%H%M%S`.jpg
> [root at siseg root]# echo $fec
> cam2005051255702.jpg
> [root at siseg root]# mv hola.txt grabacion/$fec
>
> I need to do this from Python but I'm having problems with the Linux
> variable
Hi Alberto,
[Tangent: are you running things using the Unix 'root' account? If so,
you may want to consider using a regular user account unless you really
know what you're doing. Running things as root should never be taken
lightly.]
You may want to consider using the 'datetime' module for this one. There
are portability and security issues that you may not be considering
whenever you "shell" out. If you use Python's Standard Libraries, you can
avoid these issues.
I think you can avoid using 'date' by taking advantage
of datetime.datetime.strftime():
######
>>> import datetime
>>> datetime.datetime.today().strftime("%Y%m%d%H%M%S")
'20050513135531'
######
This is portable on any system that supports the Python Standard Library.
The problems you are running into with os.system() are related to those
that other people have been asking about this week. It's sorta funny how
these questions come in streaks. *grin*
For more information on driving external processes with Python, you may
want to look at the messages on "Pipe variable to external command" that
Jeffrey was asking about yesterday:
http://mail.python.org/pipermail/tutor/2005-May/038341.html
http://mail.python.org/pipermail/tutor/2005-May/038343.html
I hope this helps!
More information about the Tutor
mailing list