-
Notifications
You must be signed in to change notification settings - Fork 265
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #200 from onkar-kota/feat/recently_accessed_files
Added recently_accessed_files module (issue #198)
- Loading branch information
Showing
3 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |