0

I have a filename like "Nena - Nur getraeumt (1982) HD 0815007.mp3" - I can see it in the directory , however in a python subprocess - my code says the file isn't found. Is there a way to bypass or encode the string in such a way that it can find the file? Here is my code fragment:

try:
    p = subprocess.Popen(["avconv" , "-y" , "-i" , upload_music_file , "-acodec" , "pcm_s16le" , process_file],  universal_newlines=True, stdout=subprocess.PIPE)
    out, err = p.communicate()
    retcode = p.wait()
except IOError:
    pass

Assuming upload_music_file is "Nena - Nur getraeumt (1982) HD 0815007.mp3" and process_file is "proccess_music.mp3"

I get the error:

avconv version 0.8.12-6:0.8.12-1, Copyright (c) 2000-2014 the Libav developers built on Jun 1 2014 17:03:01 with gcc 4.7.2 /music/Nena - Nur getraeumt (1982) HD 0815007.mp3 : No such file or directory

My only clue is when trying to ls the file from bash it get: -bash: syntax error near unexpected token `('

is there a way to process the upload_music_file string so it'll work?

Fight Fire With Fire
  • 1,626
  • 3
  • 19
  • 28
  • 2
    You have to quote the filename for it to be interpreted as a string parameter of your command in your shell: ls "/music/Nena - Nur getraeumt (1982) HD 0815007.mp3" – Antoine Pietri Dec 29 '14 at 23:35

2 Answers2

2

In my case, if the file isn't being found when using python - check for special non-printing characters in the filename.

Remove all Carriage returns and Newlines and Linefeeds from the filename and you can check for other characters on linux with ls -b to see what needs to be escaped.

Fight Fire With Fire
  • 1,626
  • 3
  • 19
  • 28
0

So, typically, when using Popen or something similar, you don't want to rely on relative paths. Since it exists, I'm going to go ahead and guess that this error is not a typo, but instead suggest that you use an absolute path rather than a relative path, which is a very common issue.

Specifically, if you currently access the file as:

Nena - Nur getraeumt (1982) HD 0815007.mp3

I would suggest, instead calling it as something similar to:

/home/user/music/Nena - Nur getraeumt (1982) HD 0815007.mp3

But again, only you would know the actual absolute path.

Additionally, I would suggest removing spaces from you filename, as they are far more pain than they are worth, but that's a matter of personal preference.

As far as the bash error, that's very common when dealing with filenames with special characters. You either have to escape each special character with a backslash, or change your filenames to something more reasonable:

Escaped: Nena\ -\ Nur\ getraeumt\ \(1982\)\ HD\ 0815007.mp3
More reasonable: Nena_Nur_getraeumt_1982_HD_0815007.mp3
Slater Victoroff
  • 21,376
  • 21
  • 85
  • 144
  • @PadraicCunningham I was assuming that the passed file was actually /music/Nena, since I don't believe I've ever seen /music as a root directory, but I could totally be wrong. Hard to debug without knowing more about the directory structure here, but I still think it's very possible that this is the issue. `ls'ing` the file is causing an error just because the parens and spaces aren't escaped. – Slater Victoroff Dec 29 '14 at 23:48
  • @PadraicCunningham That is, slightly less delicately what I was saying. – Slater Victoroff Dec 29 '14 at 23:52
  • When using `subprocess` in Python, you don't have to worry about special characters in the filename; Python takes care of quoting for you. – augurar Dec 30 '14 at 00:48
  • @augurar That's why it was an addendum and not the core answer. It does take care of it, but lower in OP's question a bash syntax error was noted, which is certainly due to the special characters. – Slater Victoroff Dec 30 '14 at 00:49
  • True, but the bash syntax error was a "red herring", i.e. it was unrelated to the problem in the Python code. – augurar Dec 30 '14 at 00:52
  • @augurar Still a good comment on general coding practice though. That was all I was aiming to add. – Slater Victoroff Dec 30 '14 at 00:54
  • 2
    I found the issue! The file name had a invisible carriage return on it that i didn't even see with a ls -b or ls -q ! – Fight Fire With Fire Dec 30 '14 at 13:48
  • So glad that you found the issue! Also, @augurar, seems like it wasn't a red herring afterall. – Slater Victoroff Dec 30 '14 at 20:09
  • @SlaterTyranus Yes it was, bash was complaining about unescaped parentheses in the filename which do not make a difference in the Python code. – augurar Dec 30 '14 at 20:19
  • @augurar yes, but the invalid syntax error was still valid and special characters were in fact the issue, even if it was a separate one. If the paren weren't there, the carriage return would yield the same error. – Slater Victoroff Dec 30 '14 at 20:29