0

I'm writing a Python script, here's the part of the script that does not work.

command = "mv " + cwd + sys.argv[2] + " wallpaper.jpg" + " ~/Pictures/Wallpapers/"
print(command)
subprocess.run(command.split(" "))

If I copy and paste the command printed from the script in the terminal it works, but when called from my Python script I get the error:

mv: target '~/Pictures/Wallpapers' is not a directory

What's causing this?

If needed, I'm using Python 3.8.10 and Ubuntu 20.04.3.

BaridunDuskhide
  • 107
  • 1
  • 5
  • 2
    [This](https://stackoverflow.com/a/10487862/13145954) should help you understadn what's going on, and [`os.path.expanduser`](https://docs.python.org/3/library/os.path.html#os.path.expanduser) should solve your problem by replacing `~` with the absolute path to the current user's home directory. – Seon Oct 22 '21 at 15:42
  • `~` is normally replaced by the shell before the `mv` command is executed. In a terminal, run `set -x` and then your command, and you will see what actually gets executed. In a script you have to expand it yourself, see [Seon](https://stackoverflow.com/users/13145954/seon)' s [comment](https://stackoverflow.com/questions/69679587/python-script-errors-out-with-mv-target-is-not-a-directory-but-command-called#comment123164292_69679587). – Bodo Oct 22 '21 at 16:13

0 Answers0