0

I have a script on a RHEL 7.x machine written in Python3. In testing this script I created a function which will append to a text file in the same directory.

If I execute the script from the local directory ie - ./pyscript.py everything works as expected.

But I am trying to execute this from a Bash script a couple directories higher and it doesn't seem to work right. The other functions in the script will execute, but this very last one which appends to a text file will not.

Now, if I run the script as the user which owns it(and the txt file) from my home dir, the script errors out with a permission error. BUT if I run the script with sudo it finishes with NO error, However it does NOT write to the text file.

My user has RW privileges on every dir between the bash script and the python script.

Any thoughts on why a sudo or local user run doesn't seem to let me write to the text file??

  • Can you add the relevant bits of code from both the bash and python scripts? – Haxiel Apr 25 at 17:14
0

I got this worked out. The issue was that when you execute a script from another directory, the python script treats the executing directory as the local directory. So in my code where I had:

with open('myfile.txt', 'a') ...

It wasn't trying to open the actual file but trying to access one that doesn't exist in the directory where I am executing the command from.

To fix just use the full path for the text file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.