0

I have written a Python program that reads multiple files and looks for some information.

Here is the command that I used:

try:
    with open(file_path, "r") as file_object:
        # do some stuff
except:
    # do some stuff

It works correctly as I wanted.

I have noticed that after running the code, the file properties Accessed changed for all the files that the code read, though code just read the file.

Interestingly, I ran the same program in a different machine with Python 3.7 and the file properties didn't change. I couldn't remember the actual version of the previous Python but it surely changed the file Accessed properties to the time I ran the code.

What is the reason behind this? Did an older version of Python change the file Accessed properties when the code read a file but the new version doesn't?

In both cases Windows 10 OS was used, files were text files. For the later case, file system is NTFS.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
abhimanyue
  • 196
  • 1
  • 12
  • I think this is platform-dependent. What OSs did you run it on, and what filesystems are the files on? [Here's some info for Windows](https://devblogs.microsoft.com/oldnewthing/20111010-00/?p=9433). – wjandrea Jan 06 '22 at 19:50
  • 1
    Sidenote: [a bare `except` is bad practice](/q/54948548/4518341). Instead, use the specific exception you're expecting like `except OSError`, or at least `except Exception`. – wjandrea Jan 06 '22 at 19:51
  • 1
    On Linux, most file systems have three attributes: Created, Modified, and Accessed. "Modified" means opened with "w" permission. "Accessed" means opened with "r" permission. – Tim Roberts Jan 06 '22 at 19:56
  • I modified the question. @wjandrea – abhimanyue Jan 06 '22 at 20:01
  • This is Linux info, but I guess the situation is similar in the Windows world. Changing the access time on disk is a performance drawback and if this information is not needed, the admin may choose to disable it on a filesystem entirely or reduce the frequency of updates (for details see mount options ending with ...atime) – VPfB Jan 06 '22 at 20:36

0 Answers0