Re: [Tutor] subprocess.call not formatting date
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 On 08/07/14 18:12, Peter Otten wrote: > 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]) Thank you. That worked perfectly. Once it's pointed out it is obvious to use the python solution, but my brain is becoming less agile as I collect more birthdays! Bob - -- Bob Williams System: Linux 3.11.10-17-desktop Distro: openSUSE 13.1 (x86_64) with KDE Development Platform: 4.13.2 Uptime: 06:00am up 2 days 9:43, 5 users, load average: 0.00, 0.02, 0.05 -BEGIN PGP SIGNATURE- Version: GnuPG v2.0.22 (GNU/Linux) Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/ iEYEARECAAYFAlO8Nw8ACgkQ0Sr7eZJrmU6HVwCaAkT+nqxn818s1Di8mgqc9U1a qksAni7exn27xTGgDV2O6vSNtg8FbgMK =9I7G -END PGP SIGNATURE- ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] subprocess.call not formatting date
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 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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] subprocess.call not formatting date
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 > 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]) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor