[Tutor] subprocess.call not formatting date

Peter Otten __peter__ at web.de
Tue Jul 8 19:12:42 CEST 2014


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

That's because the second argument to call() is bufsize, and you are passing
it the string "subvolume". While I suppose that

# untested
subprocess.call("btrfs subvolume snapshot /home/bob/A3/documents /home/bob/A3/docsnaps/`date +%y-%m-%d_%H-%M`", shell=True)

would work I suggest that you calculate the folder name in Python instead:

# untested
name = datetime.datetime.now().strftime("%y-%m-%d_%H-%M")
destpath = os.path.join("/home/bob/A3/docsnaps", name)
subprocess.call(
    ["btrfs", "subvolume", "snapshot", "/home/bob/A3/documents", destpath])




More information about the Tutor mailing list