Can I change creation date of some file using Python in Linux?
Linux and Unix file system stores :
File access, change and modification time (remember UNIX or Linux never stores file creation time, this is favorite question asked in UNIX/Linux sys admin job interview)
Understanding UNIX / Linux file systems
You can use os.utime to change access and modify time but not the creation date.
I am not a UNIX expert, so maybe I'm wrong, but I think that UNIX (or Linux) don't store file creation time.
Check out os.utime
os.utime(file_path,(new_atime,new_mtime))
It's no longer true that Linux doesn't support creation time. See:
https://askubuntu.com/questions/470134/how-do-i-find-the-creation-time-of-a-file
Note this specific answer to view C-code that displays the field:
https://askubuntu.com/a/980750/116108
If the author of that post can help I might be able to create a wrapper with ctypes to modify it from Python.
Related
I need to access the date and time in ubuntu from a program.This program may not use any commands to do this. So making a call to date is not an option.
Is there a file or files which hold this information?
Where can it be found ?
No, read time(7). There are some system calls (listed in syscalls(2)...) to query the time (since Unix Epoch); in particular time(2) and clock_gettime(2).
You then need to convert that time into a string, probably using localtime(3) then strftime(3). That conversion use some files notably /etc/timezone (and some under /usr/share/zoneinfo/ ...) according to TZ variable (see environ(7) and locale(7)).
BTW, date is free software (so you could study its source code). And you could strace(1) it.
See also vdso(7) and this.
I am trying to create a script to make an edit to the window's registry. As a fall back, I want to create a back up of the registry and save it in the working directory (or some other directory, but that is for later). Is there a way to use the power of python to backup the registry first?
So far the only way I have found to do this might be a call out to reg.exe, but I was looking for something more native to python itself.
Thanks!
The registry is a deeply Windows-centric construct, though I have not done any research on the subject, I would bet that there is no "native" way for backing up the registry in Python. I think you already have your answer and creating a process in Python and letting it run Reg Export is the best way to accomplish what you want.
However, if for some reason you don't want to run Reg.exe or invoke any external processes, I recommend that you write and save every registry entry before you edit it into a .reg file like this:
[HKEY_LOCAL_MACHINE\SOFTWARE\Adobe\Adobe Acrobat]
[HKEY_LOCAL_MACHINE\SOFTWARE\Adobe\Adobe Acrobat\9.0]
[HKEY_LOCAL_MACHINE\SOFTWARE\Adobe\Adobe Acrobat\9.0\Installer]
"AppInit_DLLs"="acaptuser64.dll"
This approach will ensure that you don't rely on any external utility and is the nearest thing to a "native" registry backup in Python.
The recommended way to do this is to create a "restore point", which will make a backup to which you can restore the registry. I don't what the API to do this is, but I'm pretty sure it exists.
You can also do it manually, of course, but that is a different issue.
You can use the winreg module's SaveKey function if the program is UAC elevated:
import winreg, win32security, win32api # use _winreg for older versions of Python
with winreg.OpenKey(winreg.HKEY_CLASSES_ROOT, '') as handle: # Replace with the desired key
win32security.AdjustTokenPrivileges(win32security.OpenProcessToken(win32api.GetCurrentProcess(), 40), 0, [(win32security.LookupPrivilegeValue(None, 'SeBackupPrivilege'), 2)]) # Basically, adjusts permissions for the interpreter to allow registry backups
winreg.SaveKey(handle, 'C:\\REGBACKUP') # Replace with the desired file path
You can then load it for use with the winreg library:
import winreg
with winreg.OpenKey(winreg.HKEY_CLASSES_ROOT, '', 'C:\\REGBACKUP') as handle:
...
The content of the file or the picture should show up in a new window.
That will depend a lot on your operating system, since there are different programs on different systems to view images, etc. But one trick you might use is
import webbrowser
webbrowser.open(THE_FILE)
That should open up your default web browser pointed to the file, which will display images, and might do what you want for certain types of files.
you could try
os.system("fspot picture.jpeg")
But, obviously, I'm assuming you're using fspot to view images, and that might only work in linux.
Check out the documentation for os.
-EDIT-
Mu Mind's solution works pretty well in Ubuntu Karmic. Not sure what it will do on a windows machine.
you can use
os.system("start "+"anyfile.txt")
assuming you have windows. This basically opens the file in it's extension (For example if you had a .txt it would open in notepad.)
Is it possible to create read only files in python which can not be changed later and in which users can not change its attribute from read-only to normal file?
Please suggest.
Thanks in advance.
This is not python specific.
If the files are made by a different user that the one viewing it the script can make it read-only. As the file is owned by the python user, the viewing user cannot just change the attributes.
So it's very much an OS question, and not a Python question.
Oh, and there is no way to prevent an administrator changing the file, or for the file to be readable but not copyable.
This is just impossible.
Any user with administrative rights can remove readonly restrictions of any kind.
Another option might be "Write a python program to kill all users over the worls so that they would not be able to change file attributes or security settings" :-)
Take a look at os.chmod() function and execute it with appropriate parameters (filename, stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH) for your just created file.
On linux other users then you will not be able to change file or change attributes to writable.
Some root user or someone logged into you account will be able to change it though.
I have a text file being written by another process on a server which I want to watch for changes. Each time a change occurs I'd like to read the new data and send it to client .
Any suggestions will be valuable . Using Django,Python
Tazim.
If you're using a recent Linux you should look into this:
http://trac.dbzteam.org/pyinotify
How do I watch a file for changes?
goes into details for win32
On Linux a convenient interface for file modification monitoring is Gamin the File Alteration Monitor. Python bindings are provided and they are really easy to use, though they could be documented better (especially the 'magic numbers' returned).