python shutil.move not working when moving from C to C but D to C

Question:

I am working on a simple installer for a app but I am getting errors.
Code looks like:

import shutil
import os

name = os.getlogin()

source_path = os.path.abspath('foo.bar')

destination_path = os.path.join('C:', 'Users', name, 'AppData', 'Roaming', 'Microsoft', 'Windows', 'Start Menu', 'Programs', 'Startup')

shutil.move(source_path, destination_path)

it works perfect for moving foo.bar from my d drive to c drive but not c drive to c. if i try to do c to c it gives me this error:

Traceback (most recent call last):
  File "D:PythonLibshutil.py", line 825, in move
    os.rename(src, real_dst)
FileNotFoundError: [WinError 3] The system cannot find the path specified: 'C:\Users\Katte\foo.bar' -> 'C:Users\Katte\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:UsersKatteOneDriveDesktopdownload.py", line 19, in <module>
    shutil.move(source_path, destination_path)
  File "D:PythonLibshutil.py", line 845, in move
    copy_function(src, real_dst)
  File "D:PythonLibshutil.py", line 436, in copy2
    copyfile(src, dst, follow_symlinks=follow_symlinks)
  File "D:PythonLibshutil.py", line 258, in copyfile
    with open(dst, 'wb') as fdst:
         ^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: 'C:Users\Katte\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup'

I’ve tried os.rename() os.replace() and compiling it into a exe and running it a administrator but none of them have worked and I am clueless.

Asked By: SleepyJesper

||

Answers:

try using this line you forgot to add \ after C: -> C:\ is the change

destination_path = os.path.join('C:\', 'Users', name, 'AppData', 'Roaming', 'Microsoft', 'Windows', 'Start Menu', 'Programs', 'Startup')
Answered By: Sagun Devkota
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.