0

I found a script online which converts a file from one specific extension to another, I am trying to utilize subprocess.call and have had no luck so far.

I am trying to use subprocess.call to convert a file in a directory. I successfully managed to perform this in the terminal and am now trying to execute this in python as part of a program.

For the arguments, I am giving 'python' the name of the script, the file I want to convert and then the new name once it has been converted.

subprocess.call(["python", "converter.py", "file1.txt", ">", "file1converted.xml"])

Am I using subprocess.call in the correct manner? I can't seem to find anywhere on how to perform what I'm trying to do, also if possible I would like to find out a way on how I can convert a file without specifying the new name after conversion, but instead using change the 'file1.txt' to '.xml' once done.

saeed foroughi
  • 1,662
  • 1
  • 13
  • 25
N2This
  • 41
  • 7
  • Could you provide some additional clarity into how it's not working correctly? Are you getting an error/exception? Is it just not behaving as you'd expect? – RagingRoosevelt Jan 28 '20 at 18:47
  • 1
    Redirecting the stdout does not work like this, have a look at this answer: https://stackoverflow.com/a/2813530/3398271 and the documentation https://docs.python.org/3/library/subprocess.html#subprocess.CompletedProcess.stdout – Attilio Jan 28 '20 at 19:11
  • In Python, it's not necessary to execute another Python script - you ought to be able to import your child script, then call the same method your child script calls. – Alastair McCormack Jan 28 '20 at 20:25
  • See also https://stackoverflow.com/questions/41470965/subprocess-popen-shell-true-to-shell-false – tripleee Jan 28 '20 at 20:42

1 Answers1

-1

There are two ways to do this, the first is the way you did it but you should add shell=True among the parameter. Do not use shell=True if the shell command is coming from an outside source, cause that would a huge security risk

subprocess.call(["python", "converter.py", "file1.txt", ">", "file1converted.xml"], shell=True)

The other way do this is to make use of Popen which I PERSONALLY prefer

subprocess.Popen(["python", "converter.py", "file1.txt", ">", "file1converted.xml"])
maestro.inc
  • 796
  • 1
  • 6
  • 13
  • The list notation is wrong with `shell=True`; you should simply pass the command line as a single string for the shell to execute. The second variation doesn't work at all (no shell means no redirection). – tripleee Jan 28 '20 at 20:40
  • @tripleee you can do it that way(as a string) or you can decide to use the list notation and it still works(I have used this a several different occasions). You should try out things before jumping to hasty conclusions. – maestro.inc Jan 28 '20 at 23:41
  • The incorrect syntax happens to work on Windows, out IMHO it should simply be an error which Python ought to reject or at least warn about. I assure you these are not hasty conclusions. Maybe examine some of my other answers in the [tag:suprocess] tag. – tripleee Jan 29 '20 at 03:13
  • Recent versions of the `subprocess` documentation specifically advice you to avoid `Popen()` in scenarios which the higher-level wrapper functions like `subprocess.run()` or the now obsolescent `subprocess.call()` can handle. – tripleee Jan 29 '20 at 03:15
  • `subprocess.call()` is obsolete like you have said, however, I find that `subprocess.run()` doesn't run properly some times if the shell isn't set to true `shell=True` which I think is a huge security risk, hence I scarcely to never apply it but `subprocess.Popen()` does not have such problem in my experience. Finally, I prefer the string format like you said but that does not take away from the validity of the list notation (even if just in windows) at that point your response to my answer at best is an opinion. – maestro.inc Jan 29 '20 at 10:28
  • The list notation is mandatory when you don't have `shell=True` and wrong when you do. The behavior on Windows is less visibly broken but still arguably wrong. Your problem with `subprocess.run()` is probably related to this misunderstanding, not a problem with the library itself. – tripleee Jan 29 '20 at 10:30
  • Personally I prefer the string type of passing argument into `subprocess` as well but the question was coined in list notation and there was no valid reason to change it IMO. – maestro.inc Jan 29 '20 at 10:32
  • I do not use list notation in my code when working with `subprocess` and `subprocess.run()` still behave strangely sometimes, this is not due to any misconception, you should really stop arriving at unfounded conclusions – maestro.inc Jan 29 '20 at 10:33
  • If you are talking about something else than the following, I am afraid I don't understand. https://stackoverflow.com/questions/35028357/difference-between-whole-string-command-and-list-of-strings-in-popen – tripleee Jan 29 '20 at 10:35