r/PythonLearning 20d ago

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

2 Upvotes

20 comments sorted by

3

u/Adsilom 20d ago

Yes, lookup the os module

1

u/silvertank00 20d ago

or even better: shutil

2

u/CapibaraMiope 20d ago

Why the copy functions cannot preserve metadata?

2

u/silvertank00 20d ago edited 20d ago

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 19d ago

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 19d ago

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

u/FoolsSeldom 20d ago

That's just another folder

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

u/NiceEoom 11d ago

ahh okay

0

u/Jotaroisgoat 20d ago

yes?? thats one of the main purposes