Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Portable way of determining if a process can move/delete a file?

What would be a portable way to determine if a Python process can move/delete a file without having to move/delete the file in question?

Use case: I'd like to inform the user of a script whether or not move/delete operations will succeed/fail prior to starting processing.

If there is a solution that only works in Linux, I'd be OK with that for the moment.

Thanks.

update: I understand that os.access can be used but is limited to real uid/gid.

like image 998
jldupont Avatar asked Jan 27 '12 13:01

jldupont


People also ask

What are the three ways in which a file can be deleted?

Browse File Explorer, and once you find the file to be deleted, right click, and click on "Delete", or press the Delete key, or drag the file to the Recycle Bin.

Does Windows 10 have a file shredder?

Does Windows 10 Have a File Shredder? Windows do not come with a built-in file shredder. It cannot shred and delete a file in its entirety. It, however, has a built-in Command Prompt command to erase an entire disk or drive.


1 Answers

Would opening the file for append work?

try:
    open(filename,'a').close()

...and catch any exception indicating that failed?

Use with caution, I'm really not sure that wouldn't do anything nasty by mistake. For example at least temporarily you'll have locked the file, and I don't know what that would do to a binary file.

like image 170
Sideshow Bob Avatar answered Oct 05 '22 10:10

Sideshow Bob