The method .stat
object pathlib.Path
brings the information in the form of a Namedtuple (basically, brings the attributes):
In [1]: from pathlib import Path
In [2]: Path("teste.txt").stat()
Out[2]: os.stat_result(st_mode=33188, st_ino=5512541, st_dev=64770, st_nlink=1, st_uid=1301285014, st_gid=1301283329, st_size=0, st_atime=1580502110, st_mtime=1580502110, st_ctime=1580502110)
In [6]: info = Path("teste.txt").stat()
In [7]: from datetime import datetime
In [8]: datetime.fromtimestamp(info.st_atime)
Out[8]: datetime.datetime(2020, 1, 31, 17, 21, 50, 59767)
In versions prior to Python 3.5, m odulo pathlib did not exist, and it is possible to get the same answer by calling the function stat
in the module os
passing the file as a string:
In [10]: os.stat("teste.txt")
Out[10]: os.stat_result(st_mode=33188, st_ino=5512541, st_dev=64770, st_nlink=1, st_uid=1301285014, st_gid=1301283329, st_size=0, st_atime=1580502110, st_mtime=1580502110, st_ctime=1580502110)
The advantage of using the pathlib.Path
is that Python already knows that it is not just a string: it is a path to a file, and the object itself has several methods, such as open
, read_text
, rename
, greatly simplifying any work that will be done with files.
But the stat
only read the file information, available on the file system, it does not serve to mark any of these properties. You can either make your tags on the file name itself, or keep a file in parallel, in the same directory, where you serialize a Python dictionary with all the information you want about the other files in the directory.
updating
The question was edited after I answered and AP’s intentions became clearer - I commented on the question explaining a few things better after this issue - I’m updating the answer to include the content of the comments:
There is no way, independent of the arbitrary system, of attaching arbitrary metadata to a file. The existence of this meta-information depends not only on the operating system, but on the file system. - It is even possible to "hide" too much information in the filesystem that you can find with your own program - but the Windows GUI will not show this information.
Example of how to "encode the information there: you could agree that if the change timestamp of a file ends in a second pair, it has been sent, if it ends in a second odd, it has not been sent. It is possible to change this timestamp - but extracting this information and showing in the native windows window does not give - one would have to use a program of his to see the extra information about the file. And then, it’s easier to just keep the information in a parallel archive - no hacking required. -
As asked in the question - on Linux, at least on the most common filesystems, this is possible yes, and the features are available in Python in the functions os.setxattr
, os.listxattr
, os.getxattr
and os.removexattr
documented here: https://docs.python.org/3/library/os.html#linux-Extended-Attributes
What kind of information do you need? Something like => https://docs.python.org/3/library/os.html#os.stat_result
– user8545
The function
os.listxattr()
is marked as Linux exclusive: https://docs.python.org/3/library/os.html#linux-Extended-Attributes– Augusto Vasques
@Diogo changed the question. He managed to understand?
– Johhny
@Augustovasques And how I would do the same with Windows?
– Johhny
The same answer Diogo gave
os.stat()
or something closer to OS likewin32api.GetFileAttributes()
– Augusto Vasques
as it is in my answer: there is no option to attach arbitrary metadata to a file, - It is even possible to "hide" too much information in the filesystem that you can find with your own progamma - but the Windows GUI will not show this information
– jsbueno
Example of how to "encode the information there: you could agree that if the change timestamp of a file ends in a second pair, it has been sent, if it ends in a second odd, it has not been sent. It is possible to change this timestamp - but extracting this information and showing in the native windows window does not give - one would have to use a program of his to see the extra information about the file. And then, it’s easier to just keep the information in a parallel file - without needing hacks.
– jsbueno
What makes me upset is that in linux is super easy to do, but in windows apparently there is no way
– Johhny