119k views
2 votes
Create a folder on your computer called "week3"

Create a script IN PYTHON that, when run, checks every second indefinitely for a new file in that folder
If file found,report back to the user:
1.File found
2. What type of file it is
3.When it was put there

User PitaJ
by
7.8k points

1 Answer

4 votes

Final answer:

A Python script was provided to create a 'week3' folder, monitor it for new files, and report details about any new files found, including type and time added.

Step-by-step explanation:

To address the student's request, we'll provide a Python script that accomplishes the following:

  1. Creates a folder named "week3".
  2. Contains a loop that monitors the folder for new files.
  3. Reports back details about any new files found.

The Python script uses the os and time modules for creating the folder and checking file presence, and the os.path module for retrieving file details. Here is an example script:

import os
import time

folder_name = 'week3'
if not os.path.exists(folder_name):
os.mkdir(folder_name)

observed_files = set(os.listdir(folder_name))

while True:
current_files = set(os.listdir(folder_name))
new_files = current_files - observed_files
if new_files:
for file in new_files:
file_path = os.path.join(folder_name, file)
file_info = os.stat(file_path)
print('File found:', file)
print('Type of file:', file.split('.')[-1])
print('Time put there:', time.ctime(file_info.st_ctime))
observed_files = current_files
time.sleep(1)

The script continuously checks for new files and provides the user with information about them.

User Ndech
by
8.2k points