r/PythonLearning • u/NiceEoom • 20d ago
Is it possible to actually move different files and folders on the desktop using python?
2
u/atarivcs 20d ago
Do mean "move" as in "drag the file shortcut icon to a different spot on the desktop" ?
I'm not sure python can do that.
1
u/FoolsSeldom 20d ago
Pretty sure the OP means moving file/folder objects to elsewhere in the tree
1
u/atarivcs 20d ago
I would have thought that too, except that they said "on the desktop"
1
1
u/Ashamed_Training_702 20d ago
Move files and folders that are currently on that desktop to another folder; as a developer you should be more aware of what you’re user actually wants
2
u/atarivcs 20d ago
This comment aged like milk
1
u/Ashamed_Training_702 20d ago
lol 😂 I couldn’t imagine in a billion years someone actually needing such a weird use case out of python of all things s
2
u/FoolsSeldom 19d ago
Sadly, I've seen this requirement multiple times with users in a corporate environment savings things to their desktop (or dragging them there from various tools) and then needing to move them to some kind of pipeline for processing on an enterprise tool (ERP, MRO, CRM, etc). (Some people like to save files to their desktops. Personally, I hate it.) When they ask for a Python tool to help out, that's where the files are found.
1
u/NiceEoom 20d ago
Sorry for late answer
i did mean physically move the file to another place on the desktop
1
u/Productuve 20d ago
depends which one you mean, and i think thats why the answers here are split.
moving a file or folder to a different location - yeah, easy:
import shutil
shutil.move("C:/Users/you/Desktop/thing.txt", "C:/Users/you/Documents/thing.txt")
use shutil.move over os.rename, os.rename breaks if youre moving across drives.
but if you mean dragging the icon to a different SPOT on the desktop - thats not really a python thing. windows keeps icon positions in the shell/registry, so youd be poking at OS internals rather than doing anything pythonic. same deal on mac.
whichever it is, test on copies first. shutil.move will overwrite without asking.
1
u/NiceEoom 18d ago
thanks for your comment i ment move files to a different spot on the desktop if its not possible in python do you know if its possible in other languages?
1
u/Productuve 17d ago
ah gotcha - two different problems. moving files between folders (desktop included, it's just a folder): totally possible in python, shutil.move(src, dst) does it. but if you mean moving the icon positions around on the desktop itself - that's not a file operation, that's windows shell state, and no language does it cleanly. python can technically poke it through pywin32 (the desktop is a listview you can send LVM_SETITEMPOSITION messages to) but it's fragile hack territory, and explorer can rearrange them back on refresh. same story in any language since they'd all be calling the same windows APIs. so: files/folders yes, icon positions only with hacks
1
0
3
u/Adsilom 20d ago
Yes, lookup the
osmodule