1

I am creating a program where I will convert files using subprocesses. The code I am using for the conversion is:

import tornado.ioloop
import tornado.web
import os

print "If at any point you wish to quit the program hit Ctrl + C"

filetype = raw_input("What kind of file would you like to convert? Audio, Image, Video or Document: ")

if filetype == "Document":
    path = raw_input("Please drag and drop the directory in which the file is stored into the terminal:")
    os.chdir(path[1:-2])
    filename = raw_input("Please enter the name of the file you would like to convert, including the file-type. e.g. test.txt, however please do make sure that the file-name does not have any spaces:")
    Fileextension = raw_input("What filetype would you like the program to convert your file to. E.g. .txt: ")
    from subprocess import check_call   
    check_call(["unoconv " ,"-f ", Fileextension , + filename])

elif filetype == "Audio":
    path = raw_input("Please drag and drop the directory in which the file is stored into the terminal:")
    os.chdir(path[1:-2])
    filename = raw_input("Please enter the name of the file you would like to convert, including the file-type. e.g. test.txt, however please do make sure that the file-name does not have any spaces:")
    Fileextension = raw_input("What filetype would you like the program to convert your file to. E.g. .mp3: ")
    body, ext = os.path.splitext("filename")
    check_call(["ffmpeg" ,"-i", filename , + body + Fileextension])

elif filetype == "Video":
    path = raw_input("Please drag and drop the directory in which the file is stored into the terminal:")
    os.chdir(path[1:-2])
    filename = raw_input("Please enter the name of the file you would like to convert, including the file-type. e.g. test.txt, however please do make sure that the file-name does not have any spaces:")
    Fileextension = raw_input("What filetype would you like the program to convert your file to. E.g. .mp4: ")
    body, ext = os.path.splitext("filename")
    from subprocess import check_call   
    check_call(["ffmpeg" ,"-i", filename , + body + Fileextension])

elif filetype == "Image":
    path = raw_input("Please drag and drop the directory in which the file is stored into the terminal:")
    os.chdir(path[1:-2])
    filename = raw_input("Please enter the name of the file you would like to convert, including the file-type. e.g. test.txt, however please do make sure that the file-name does not have any spaces:")
    Fileextension = raw_input("What filetype would you like the program to convert your file to. E.g. .Jpeg: ")
    body, ext = os.path.splitext("filename")
    from subprocess import check_call   
    check_call(["ffmpeg" ,"-i", filename , + body + Fileextension])

When i run the program now I get the error:

  File "conversion.py", line 15, in <module>
    check_call(["unoconv " ,"-f ", Fileextension , + filename])
TypeError: bad operand type for unary +: 'str'

Any ideas as to how i can solve this. Code would be much appreciated, but at this point any help would be much appreciated.

1
  • 1
    Remove the + before filename. Error message clearly states that. Call it like check_call(["unoconv " ,"-f ", Fileextension , filename]) Commented May 26, 2015 at 13:02

1 Answer 1

1

As the error suggests you have both a , and + in the array. Based on the other things you're doing, you probably want to get rid of the , after Fileextension. You probably want to change all those lines to something like

subprocess.check_call(['unoconv', '-f', Fileextension, filename])

Note that I also got rid of the space in "unoconv " because it will otherwise be looking for that space as part of the executable name.

When passing a list to check_call each list element is treated as an argument to the process (which is the first list element). So if you want to run unoconv -f file.ext your list for check_call becomes a 3 element list: ['unoconv', '-f', '.txt', 'file.ext']

You seem to be mixing up the string concatenation to put the extension on the filenames and building the list of arguments.

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

3 Comments

I see what you mean, but when converting something in unoconv you have to put the information as unoconv -f .txt file.ext Do you know how I would phrase that.
@AveryRipollCrocker sorry I wasn't familiar with unoconv. I have updated my answer to include the extra argument that I think is what you want
@AveryRipollCrocker if that helped, feel free to accept the answer, though if it has not solved the problem you should not do so

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.