I have a File that needs to be moved by my python script with shutil but I don't know where the origin location is, how do I find the origin location?
import shutil
import os
name = os.getlogin()
shutil.move(./file.foo, 'C:/Users/' name '/AppData/Roaming/Microsoft/Windows/Start Menu/Programs/Startup')
the "file.foo" is in the same directory as my python script
I have searched everywhere but I cant get a good result even with os.path
and others.
CodePudding user response:
os.path and os.path.abspath() method to get the absolute path to the file.
import shutil
import os
# Get the absolute path to the file using os.path.abspath()
source_path = os.path.abspath('file.foo')
# Construct the destination path using os.path.join()
destination_path = os.path.join('C:', 'Users', name, 'AppData', 'Roaming', 'Microsoft', 'Windows', 'Start Menu', 'Programs', 'Startup')
# Move the file with shutil.move()
shutil.move(source_path, destination_path)
CodePudding user response:
use shutil.move
method to move a file shutil.move(src, dst, copy_function=copy2)
Recursively move a file or directory (src) to another location (dst) and return the destination. refer
source = 'path/to/file'
destination = 'path/to/file'
dest = shutil.move(source, destination, copy_function = shutil.copytree)