Home > database >  Tar command in Python scripting fails after adding two variables
Tar command in Python scripting fails after adding two variables

Time:06-23

I'm facing an error while slightly changing the last step of my Python script, could anyone help me out?

1- Asking for input

vm = 'input your VM name'

#centos

2- Defining a path manually

path = 'var/lib/libvirt/images/centos.qcow2' 
#Directory of centos image file

3- get the date

var=os.popen('date  "%Y%m%d"')
vmdate=var.read()
#20220622

4- Create a Tar archive file that contains two files.

os.system('tar cvJf '   vm   '.tar.xz '   vm   '.xml'   ' '   path)

#create a Tar archive file that contains the centos.xml file and centos.qcow2 file. This command works fine and the output is: centos.tar.xz

BUT

5-When I tried to add the date after the name of Tar archive (20220622) I got the below error. (I want to change the Tar file name to centos20220622.tar.xz (vm vmdate.tar.xz))

os.system('tar cvJf '   vm   vmdate   '.tar.xz '   vm   '.xml'   ' '   path)

Error Message:

tar: Cowardly refusing to create an empty archive
Try `tar --help' or `tar --usage' for more information.
sh: line 1: /home/aku/bin/.tar.xz: Permission denied

CodePudding user response:

first add the forward slash..

path = '/var/lib/libvirt/images/centos.qcow2' 

and strip the \n in your date, it will work...

>>> vmdate
'20220622\n'

>>> vmdate.rstrip()
'20220622'
  • Related