Search code examples
pythonpathsubprocesspopen

Can't open filepath in Python if filepath contains "," or "="


I can't open paths and highlight files if the path contains either "," or "=" (and maybe other symbols).

This code works if the path doesn't have the characters mentioned above:

import subprocess
subprocess.Popen(r'explorer /select,' + str(Path(link)))

I don't get any error it just defaults to open "This PC".

Does anyone out there know what's wrong?


Solution

  • You can use multiple arguments with check_call instead. Python will escape them correctly:

    from subprocess import check_call
    check_call(['explorer', '/select,', str(Path(link))])