Python Forum
Command in Bash not working in python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Command in Bash not working in python
#1
I am receiving a 'No such file or directory' error on a command that is working in bash shell.
Here is the code:
#!/bin/python
from datetime import date, timedelta
import subprocess
import os
y = date.today() - timedelta(1)
strFile = 'Backup-' + y.strftime('%m-%d-%y') + '.7z'
cmdZip = '7z a ' + strFile + ' /home/dvdsrv/stores/*'
os.chdir('/home/dvdsrv/stores')
suprocess.Popen(cmdZip)
If I go through each line in the python interpreter and then check values,
os.getcwd() returns '/home/dvdsrv/stores'
and cmdZip returns '7z a -t7z Backup-01-04-18.7z /home/dvdsrv/stores/*'
And when I run that command from bash it works, but calling it from subprocess.Popen() gives me the error.
What am I missing here?
Reply
#2
Try replacing your subprocess to this

subprocess.Popen("7z a -t7z Backup-01-04-18.7z *", cwd="/home/dvdsrv/stores")
Reply
#3
(Jan-05-2018, 04:53 PM)hshivaraj Wrote: Try replacing your subprocess to this

subprocess.Popen("7z a -t7z Backup-01-04-18.7z *", cwd="/home/dvdsrv/stores")

Thanks for the reply. Unfortunately I am receiving the same error.
Reply
#4
subprocess.Popen(["7z", "a", "-t7z", "Backup-01-04-18.7z",  "/home/dvdsrv/stores/*"]).communicate()
I got this working.

And reason are in the documentation

Quote:args should be a sequence of program arguments or else a single string. By default, the program to execute is the first item in args if args is a sequence. If args is a string, the interpretation is platform-dependent and described below. See the shell and executable arguments for additional differences from the default behavior. Unless otherwise stated, it is recommended to pass args as a sequence.

On Unix, if args is a string, the string is interpreted as the name or path of the program to execute. However, this can only be done if not passing arguments to the program.
Reply
#5
The last way @hshivaraj show is the advisable way to do it.
Split command into a list then default shell=False is used.

If use one string command like in first examples then most set shell=True,
but this has security considerations.
Reply
#6
Thanks for the input!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problems passing arguments containing spaces to bash script and then on to python kaustin 6 421 Apr-03-2024, 08:26 PM
Last Post: deanhystad
  Call a bash script from within a Python programme Pedroski55 6 2,475 Dec-06-2021, 01:53 PM
Last Post: DeaD_EyE
  Showing and saving the output of a python file run through bash Rim 3 2,479 Oct-06-2021, 10:48 AM
Last Post: gerpark
  How to kill a bash script running as root from a python script? jc_lafleur 4 5,941 Jun-26-2020, 10:50 PM
Last Post: jc_lafleur
  Subprocess command working for one cmd and for cmd one not wrking PythonBeginner_2020 0 4,147 Mar-25-2020, 01:52 PM
Last Post: PythonBeginner_2020
  Long command with characters not working in Python on Oracle Linux 7 iaas_infra 10 6,266 Jul-19-2019, 04:53 PM
Last Post: ichabod801
  Trying to convert my Expect/bash to Python sumncguy 4 4,052 Jun-07-2019, 07:14 AM
Last Post: DeaD_EyE
  Make bash call my own Python modules Pedroski55 10 5,174 Apr-22-2019, 04:04 PM
Last Post: snippsat
  Linux command output not working adam2020 2 2,719 Mar-23-2019, 01:20 PM
Last Post: adam2020
  t.onkey command isn't working PythonSnake 2 3,059 Jan-24-2019, 08:02 PM
Last Post: PythonSnake

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020