Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:\Python\Lib\shutil.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:\Users\Katte\OneDrive\Desktop\download.py", line 19, in <module>
    shutil.move(source_path, destination_path)
  File "D:\Python\Lib\shutil.py", line 845, in move
    copy_function(src, real_dst)
  File "D:\Python\Lib\shutil.py", line 436, in copy2
    copyfile(src, dst, follow_symlinks=follow_symlinks)
  File "D:\Python\Lib\shutil.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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading