0

I am very new to shell scripting, so I'm still figuring things out. Here is my problem:

I have a python .py executable file which creates multiple files and saves them to a directory. I need to run that file in a shell script. For some reason, the shell script executes the python script but no new files appear in my directory. When I just run the .py file, everything works fine

Here's what my shell script looks like:

 #!/bin/bash cd /home/usr/directory python myfile.py 

Within my python script, the files that are saved are pickled object instances. So every one of them looks something like this:

 f = file('/home/usr/anotherdirectory/myfile.p','w') pickle.dump(myObject,f) f.close() 
2
  • Stupid question, does myfile.py reside in /home/usr/directory? I know the level of stupidity on this one, just making sure. Also there should be no space between #! and /bin/bash :) And if you think you need the bash script for running this as a cron or a part of a installation process, just add #!/usr/bin/python to the top of your script and chmod +x myfile.py and you can execute it just like it would be a bash-script :) Commented Jun 3, 2014 at 19:03
  • myfile.py resides in the directory. The files that it creates are actually supposed to be saved to another directory. And my bad on the space, I don't have it in my script, just accidentally put it here. And the reason I need to put it in a bash script is because I need to submit it as a batch job so it has to be in a shell script format. Commented Jun 3, 2014 at 19:05

2 Answers 2

4

This line:

f = file('/home/usr/directory/myfile.p','w') 

Should be:

f = open('/home/usr/directory/myfile.p','wb+') 

For best practices it should be done like this:

with open('/home/usr/directory/myfile.p','wb+') as fs: pickle.dump(myObject, fs) 
Sign up to request clarification or add additional context in comments.

2 Comments

wb+ just means you open it in read+write binary form. How would this solve anything? Edit: noticed that it sad file( and not open( now, that would solve it yes.
@Torxed: He also changed file to open.
0

The documentation for the file function states:

When opening a file, it’s preferable to use open() instead of invoking this constructor directly.

Problems like this may be one of the reasons why. Try changing

f = file('/home/usr/directory/myfile.p','w') 

to

f = open('/home/usr/directory/myfile.p','w') 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.