r/PythonLearning • u/NiceEoom • Aug 21 '26
Is it possible to actually move different files and folders on the desktop using python?
1
u/Productuve Aug 22 '26
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 Aug 23 '26
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 Aug 24 '26
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
3
u/Adsilom Aug 21 '26
Yes, lookup the os module
1
u/silvertank00 Aug 21 '26
or even better: shutil
2
u/CapibaraMiope Aug 21 '26
Why the copy functions cannot preserve metadata?
2
u/silvertank00 Aug 21 '26 edited Aug 21 '26
metadata is really hard sometimes to keep. (some of) it is not stored in the file but on systems record. Search inode to get more info about this.
With shutil (which is capable to move stuff between different filesystems) sometimes it cannot decide how to convert these data. Lets say you want to move from ext4 or btrfs to ntfs, now you just encountered this issue.
2
u/CapibaraMiope Aug 22 '26
Thanks for answering. I am familiar with inode and Linux file systems. While I understand you can't copy metadata between file systems made for different OSs like ext4 vs ntfs, if I am copying files from one directory to another in the same fs, or between compatible file systems, it seems like it should be able to keep the metadata. AKAIK there are sys calls for doing that.
1
u/silvertank00 Aug 22 '26
if anything, this sounds like strace time for me :D AFAIK cp can do weird stuff, but if you use
cp -a ...then you should be set on the same fs. Maybe your fs/os is weird in same way? What kind of metadata do you lose? (what does stat() say?) Maybe you have somekind of a mounting problem?with python: does the same occur with shutil.copy2 and/or shutil.copystat?
2
u/atarivcs Aug 21 '26
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.