-1

I was trying to write a script using Python which used an open source pen-testing tool called PRET which I got from Github. I am using PyCharm for this.

My code was:

import os
def test():
    os.system('cd PRET')
    os.system('python2 pret.py')
test()

while doing that, I encountered this error:

sh: line 0: cd: PRET: No such file or directory /System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python: can't open file 'pret.py': [Errno 2] No such file or directory

help-info.de
  • 5,228
  • 13
  • 34
  • 35
  • 1) **never** use `os.system`. It's unsafe and there are better alternatives in the `subprocess` module. 2) `os.system` spawns a new shell for each call, which means your `cd PRET` is doing nothing useful – Giacomo Alzetta Jan 08 '20 at 11:49
  • If `pret.py` is at all competently written, it does not care which directory you are in when you run it. If so, `cd there; python2 pret.py` is a needlessly complex way to say `python2 there/pret.py` – tripleee Jan 08 '20 at 11:53
  • The first error message means you do not have a directory named `PRET` in the directory where you are attempting to run this. Probably you need to understand the concept of current working directory first. https://stackoverflow.com/a/55342466/874188 has an explanation. – tripleee Jan 08 '20 at 12:04

2 Answers2

0

os.system() will not rememeber the change of directory when the first execution terminates hence why your solution is not working.

As suggested in the comments, you can just omit the cd command just execute pret.py using the absolute path:

import os

def test():
    os.system('python2 <absolute_path>/PRET/pret.py')

test()
AK47
  • 7,795
  • 6
  • 33
  • 57
  • Still shows the same error: sh: line 0: cd: PRET: No such file or directory /System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python: can't open file 'pret.py': [Errno 2] No such file or directory – Code Gatherer Jan 08 '20 at 11:56
  • Does `/PRET/pret.py` exist in the same folder as the script you are running? – AK47 Jan 08 '20 at 11:56
  • Start by doing an `os.system('ls')` or `os.system('pwd')` - My guess is that the directory you are executing `cd PRET` in is not the same directory as you think it is – AK47 Jan 08 '20 at 11:57
  • nope. let me try git cloning it to that then. – Code Gatherer Jan 08 '20 at 11:58
  • No. Just use an absolute path to whereever the script exists - see my updated answer – AK47 Jan 08 '20 at 11:59
0

The two os.system() commands are separate, so after you cd in the first, that has no effect in the second. Try this:

import os
def test():
    os.system('cd PRET; python2 pret.py')
test()
Joshua Fox
  • 15,727
  • 14
  • 65
  • 108
  • Thanks for the effort but it still shows the same error: – Code Gatherer Jan 08 '20 at 11:55
  • sh: line 0: cd: PRET: No such file or directory /System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python: can't open file 'pret.py': [Errno 2] No such file or directory – Code Gatherer Jan 08 '20 at 11:55