LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-10-2012, 08:22 PM   #1
2armz
Member
 
Registered: Sep 2012
Location: Garner NC
Distribution: Fedora 17
Posts: 35

Rep: Reputation: Disabled
Python tarball script


Hello please help

I just started learning python, and been trying to research on how to fix the problem, but have had no luck .

My goal is to make the file name of the tar file with the current date. I'm either getting invalid syntax : or no support of ("path/%s.tar") % now

Any clues, or am I way off.

Code:
#!/usr/bin/python

import os
import datetime
import tarfile


now = datetime.datetime.now()


os.chdir("/home")
tar = tarfile.open("/run/media/user/backup/backup1.tar", "w:tar") # I want to name the .tar file with the current date
tar.add("/home")
tar.add("/etc")
tar.close()
 
Old 12-11-2012, 10:54 AM   #2
audriusk
Member
 
Registered: Mar 2011
Location: Klaipėda, Lithuania
Distribution: Slackware
Posts: 360

Rep: Reputation: 199Reputation: 199
Not sure what problems you're facing as you haven't provided your faulty code and traceback which it gives you. This should work:
Code:
import tar
from datetime import date

today = date.today().strftime('%Y%m%d')

tar = tarfile.open('/path/to/backup-%s.tar' % today, 'w:tar')
tar.add('/foobar')
tar.close()
Consult strftime() docs to learn about the formats it supports.
 
Old 12-11-2012, 03:25 PM   #3
2armz
Member
 
Registered: Sep 2012
Location: Garner NC
Distribution: Fedora 17
Posts: 35

Original Poster
Rep: Reputation: Disabled
doh' I fail... my brain was shutting down when I posted that. I will try this code . Thanks, from what I see i was putting the %s and the % date in the wrong place.
 
Old 12-11-2012, 03:31 PM   #4
2armz
Member
 
Registered: Sep 2012
Location: Garner NC
Distribution: Fedora 17
Posts: 35

Original Poster
Rep: Reputation: Disabled
Yeah!!! it worked thank you so much!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to close open ports using a python script or a shell script in python ?? apanimesh061 Programming 3 11-20-2011 12:31 AM
[SOLVED] Making a SlackBuild script download from a repository instead of a tarball ahmadj Slackware 7 01-02-2011 06:48 AM
Python related: How to access a Perl script behind a firewall from Python? vxc69 Programming 8 12-14-2010 07:32 AM
Please help with tarball script mj12net_brian Linux - Server 3 12-07-2009 12:38 PM
python cgi script and premature end of script headers Neruocomp Programming 1 07-28-2005 11:43 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration