1

I am creating a Django application that takes an input file from a user. I want to use the subprocess module to take the file and pass it as an argument to an external script and take back the results. What would be the format for the subprocess.Popen call. I would like, to also pass an option to the script like -a. In other words how would a subprocess.Popen call look like for a command line that looks something like this:

./myscript -option file

Also are there any issues regarding the path of the script i am trying to run. Thanks a lot.

This is the code that I am using in my views.py. I am just trying to see if a simple cp command works and how to pass the arguments:

def upload_file(request):

    if request.method == 'POST':
    form=UploadFileForm()        
    form = UploadFileForm(request.POST, request.FILES)
        if form.is_valid():
            handle_uploaded_file(request.FILES['file'])
            return HttpResponseRedirect('/upload')
    else:
        form = UploadFileForm()
    return render_to_response('upload_file.html', {'form': form})

def handle_uploaded_file(f):

    p=subprocess.Popen(['/bin/cp',f , '/home/dutzy/Desktop'])
2
  • The path is determined by how your web server is talking to your code. You'll have to be more specific. Commented Apr 15, 2011 at 21:42
  • I am using the django development server Commented Apr 15, 2011 at 22:04

3 Answers 3

5

subprocess.Popen has a pretty understandable syntax:

subprocess.Popen(['./myscript', '-option', 'file'])

Just look through the examples and you'll get the pattern.

Sign up to request clarification or add additional context in comments.

Comments

1

You simply pass a tuple or list of your binary name (in $PATH or relative) and its arguments.

import subprocess
p = subprocess.Popen(('./myscript', '-option', 'file'))

6 Comments

I have a function handle_uploaded_file(f) where I tried a little example using an standard unix command: p=subprocess.Popen(['cp', 'f' , '/home/dutzy/Desktop/mysite']). Am i not passing the file argument correctly?
Remove quotes from f. That is: ['/bin/cp', f, '/home/dutzy....']. But don't do this, it's an awful way to copy a file. Instead, use the shutil module: docs.python.org/library/shutil.html
I get an execv() arg 2 must contain only strings error, I am not really trying to copy a file using this method. I am just trying to figure out how to pass the file argument in the subprocess call and this was the first method that came to my mind:)
Paste the full code, it's hard to tell you what you're doing wrong.
i made en edit to the initial question and paste the code there
|
1

request.FILES['file'] is not a string filename; it's an UploadedFile object.Refer to the Django docs to see what you can do with that object.

It is bad form to try to get the pathname and execute a copy since that will break for a remote user. In this usage, the web browser is uploading the file data to your server, which passes it to Django, which creates the UploadedFile to handle it. To simply copy that file to disk, you need code like:

def handle_uploaded_file(f):
    destination = open('path/to/store/at/' + f.name, 'wb+')
    for chunk in f.chunks():
        destination.write(chunk)
    destination.close()

If you don't want to use the uploaded name, use something else besides f.name. If you want to run a command on the uploaded file, first save it somewhere (maybe in a temporary file) and then use subprocess.Popen to run the command.

1 Comment

Ok thanks I will use something like this to run a command or script over the uploaded file: def handle_uploaded_file(f): destination = open('/home/.../file.txt', 'wb+') for chunk in f.chunks(): destination.write(chunk) destination.close() p=subprocess.Popen(['./myscript', '/home/.../file.txt' , 'arg', 'arg']) sorry but i couldn't answer my own question so the code would be formatted

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.