[Tutor] subprocess.call not formatting date

Emile van Sebille emile at fenx.com
Tue Jul 8 19:18:07 CEST 2014


On 7/8/2014 9:44 AM, Bob Williams wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> I'm using Python 2.7.6 on an openSUSE linux system.
>
> I'm trying to convert a shell (bash) script to a python script, and everything's worked OK except this. The following line in the shell script
>
> btrfs subvolume snapshot /home/bob/A3/documents /home/bob/A3/docsnaps/`date +%y-%m-%d_%H-%M`
>
> creates a folder of the form
>
> /home/bob/A3/docsnaps/14-07-08_17-32
>
> ie. the name is a formatted date string, which is what I want.
>
> In my python script, this
>
> subprocess.call('btrfs', 'subvolume', 'snapshot', '/home/bob/A3/documents', '/home/bob/A3/docsnaps/`date +%y-%m-%d_%H-%M`')
>
> creates a folder
>
> /home/bob/A3/docsnaps/`date +%y-%m-%d_%H-%M`
>
> In other words, it does not format the date, but takes the stuff between the backticks (`) as a string.
>
> I tried adding shell=True, but got the following error:
>
> Traceback (most recent call last):
>    File "/home/bob/bin/btrfs-backup.py", line 55, in <module>
>      subprocess.call('btrfs', 'subvolume', 'snapshot', '/home/bob/A3/documents', '/home/bob/A3/docsnaps/`date +%y-%m-%d_%H-%M`', shell=True)
>    File "/usr/lib64/python2.7/subprocess.py", line 522, in call
>      return Popen(*popenargs, **kwargs).wait()
>    File "/usr/lib64/python2.7/subprocess.py", line 658, in __init__
>      raise TypeError("bufsize must be an integer")
> TypeError: bufsize must be an integer
>
> Any suggestions, please?

Pass in the formatted string using the python datetime module.

datetime.datetime.today().strftime("%y-%m-%d_%H-%M")

see https://docs.python.org/2/library/datetime.html for more details.

Emile





More information about the Tutor mailing list