Skip to content

Commit

Permalink
Merge pull request #200 from onkar-kota/feat/recently_accessed_files
Browse files Browse the repository at this point in the history
Added recently_accessed_files module (issue #198)
  • Loading branch information
DhanushNehru authored Oct 22, 2023
2 parents 9b77b0f + 23b0ad7 commit 4ea2d1d
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
64 changes: 64 additions & 0 deletions recently_accessed_files/README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# recently_accessed_files

## Introduction

This Python script that extracts the path of recently accessed files in the specified directory.

## Usage

### Prerequisites

Before using this script, ensure you have the following:

- Python installed on your system.
- Required libraries: `os`, `datetime`

### Running the Script

1. Run this script in your terminal.

```python
$ python recently_accessed_files.py
```
2. After running the python script we have to provide the desired directory from which we want to extract the paths.

```python
$ Enter the directory path to search:
```
3. Example Output

![Alt text](image-1.png)

### Information about .py file

1. `get_file_access_time` function

- This function provides the time, in seconds since the Unix epoch, when the file was last accessed.
- Args :
- **file_path**: provided directory path
- Returns:
- access time

1. `get_recently_accessed_files` function

- This function returns a list of recently accessed files in the given directory and its subdirectories, sorted by their access time in descending order.

- Args :
- **directory_path**: The path to the directory to search.
- **time_threshold**: The time threshold in seconds since the Unix epoch. Only files accessed after the time threshold will be returned. If `None`, all files will be returned.

- Returns:
- A list of file paths, sorted by their access time in descending order.

### Output

The script will create a list of recently accessed files.

### Example

- file1.txt
- file2.txt
- file3.txt
- file4.txt
- file5.txt
- file6.txt
Binary file added recently_accessed_files/image-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions recently_accessed_files/recently_accessed_files.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import os
import datetime

def get_file_access_time(file_path):
return os.path.getatime(file_path)

def get_recently_accessed_files(directory_path, time_threshold=None):

recently_accessed_files = []
for root, dirs, files in os.walk(directory_path):
for file in files:
file_path = os.path.join(root, file)
file_access_time = get_file_access_time(file_path)

if time_threshold is None or file_access_time >= time_threshold:
recently_accessed_files.append(file_path)

return sorted(recently_accessed_files, key=lambda file_path: get_file_access_time(file_path), reverse=True)

def main():

# Get the directory path to search.
directory_path = input("Enter the directory path to search: ")

# Get the list of recently accessed files.
recently_accessed_files = get_recently_accessed_files(directory_path)

# Print the list of recently accessed files.
print("List of recently accessed files:")
for file_path in recently_accessed_files:
print(f"{file_path}")

if __name__ == "__main__":
main()

0 comments on commit 4ea2d1d

Please sign in to comment.