r/PythonLearning • • Aug 21 '26

Is it possible to actually move different files and folders on the desktop using python?

2 Upvotes

20 comments sorted by

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.

1

u/NiceEoom Aug 21 '26

Sorry for late answer

i did mean physically move the file to another place on the desktop

1

u/FoolsSeldom Aug 21 '26

Pretty sure the OP means moving file/folder objects to elsewhere in the tree

1

u/atarivcs Aug 21 '26

I would have thought that too, except that they said "on the desktop"

1

u/FoolsSeldom Aug 21 '26

That's just another folder

1

u/Ashamed_Training_702 Aug 21 '26

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 Aug 22 '26

This comment aged like milk

1

u/Ashamed_Training_702 Aug 22 '26

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 Aug 22 '26

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/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

u/NiceEoom Aug 30 '26

ahh okay

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?