2

In Filemanager (Linux and Windows) I can right click a file and see its properties. I would like to set them using Python. It seems complicated. With os.stat(file) I can retrieve standard file properties, but I can not set (custom) properties. There seems to be a solutions, for C# Retrieve metadata column feilds , and for VisualBasic How do I add a custom property to a file using VBScript but I could not find anything for Python. Your help is much appreciated. Python is polymorphous, so I would expect :

 setattr(file, property_name, property_value) 

but this does not work.

Community
  • 1
  • 1
Bernard
  • 681
  • 1
  • 8
  • 21
  • 1
    Could you provide an example of non-standard file properties that you would like to set? – jfs Mar 18 '14 at 10:49
  • Description, version, valid,etc. I am building a Sanity_check to check if all required resources, for a specific program, are met. It is a kind of log file, but the result can be read in the program again, and differences can be compared. The volume of files may be as large as of logfiles. A data file (pickle) have to have descriptive info, so the user knows he has the correct file for a specific kind of sanity_check. – Bernard Mar 18 '14 at 11:56
  • don't put the relevant info in the comments. [edit] your question instead – jfs Mar 18 '14 at 12:14
  • Do you need to access properties that you do not create yourself? – jfs Mar 18 '14 at 12:16
  • It would be useful, be not necessary. – Bernard Mar 18 '14 at 12:48
  • If it is useful to access pre-existing custom properties then you need to use the specific API. I don't think it is portable between OSes, filesystems. You could use `pywin32` or `ctypes` to access Windows API – jfs Mar 18 '14 at 12:53
  • I looked at http://docs.python.org/3.3/library/ctypes.html and Linux seemed to be possible, but it is very complicated. Setting properties for specific objects in general is easy in Python. But I am astonish that when the object is a file it gets very complicated. Is only writing the properties to a file and not automated reading, is there an easy solution? – Bernard Mar 18 '14 at 13:24
  • [`CreateObject("DSOFile.OleDocumentProperties").Open("E:\Setup.msi").CustomProperties.Add "Version", "1.0.0.0"`](http://stackoverflow.com/questions/3726043/how-do-i-add-a-custom-property-to-a-file-using-vbscript/3787416#3787416) is Windows specific. If it is complicated to reproduces these calls using `ctypes` then use `pywin32`. The code should be almost the same – jfs Mar 18 '14 at 13:31
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/49952/discussion-between-bernard-and-j-f-sebastian) – Bernard Mar 18 '14 at 14:06

2 Answers2

0

Check out this answer.

With a few modules you can change e.g. the creation time by @Claudiu's solution:

import pywintypes, win32file, win32con
def changeFileCreationTime(fname, newtime):
    wintime = pywintypes.Time(newtime)
    winfile = win32file.CreateFile(
        fname, win32con.GENERIC_WRITE,
        win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE | win32con.FILE_SHARE_DELETE,
        None, win32con.OPEN_EXISTING,
        win32con.FILE_ATTRIBUTE_NORMAL, None)

    win32file.SetFileTime(winfile, wintime, None, None)

    winfile.close()

pywin32, which includes pywintypes etc, is available from here. I'm not sure if there is specific documentation for pywin32, but these types of modules are usually wrappers around the MS API, e.g. for VBA or C#. So it's often quite easy to find answers in the e.g. MS C# docs (based on my experience with office applications). This might help you to set any arbitrary file property.

Community
  • 1
  • 1
pandita
  • 4,739
  • 6
  • 29
  • 51
  • 1
    Thanks, but I need it for all os. If only a specific os is possible then I need Linux. I see I added, by mistake, a windows tag. I just removed the windows tag. – Bernard Mar 18 '14 at 13:04
  • No worries. I don't use Linux very often, but I believe it has many command line tools that might do what you want. E.g. 'touch' changes the last accessed date. You could run this from Python maybe – pandita Mar 18 '14 at 19:19
  • It has, and I tried see http://askubuntu.com/questions/435981/viewing-custum-attributes-of-a-file-within-nautilus but without success. Most important I want to limit dependencies. Using a OLE object for just calling attr creates an undesired and unpredictable dependencies which are hard to maintain. – Bernard Mar 19 '14 at 08:36
-2

Do not try to set metadata for files in Python. Because even if it would be possible, copying the file might lose the metadata, see http://docs.python.org/3/library/shutil.html#module-shutil This mean using metadata will result to unpredictable results. Which is bad. Better something is not possible at all, then being unpredictable.

Work around : I created an addition textfile meta.txt and archived these files using :

shutil.make_archive

Not beautiful at all. But this archive file can be copied, without the change loosing metadata. When un-archived all metadata of the file are accessible.

Bernard
  • 681
  • 1
  • 8
  • 21