Provide a python 3 script to scan all files on a system regardless of OS, and place the names/mod time/file extension/size in MB in an easy to read csv file. Ignore system files and program files.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Here's a Python 3 script that scans all files on a system, regardless of the operating system, and saves the file information (name, modification time, file extension, and size in MB) in a CSV file. The script ignores system files and program files:
python
import os
import csv
import platform
import datetime
def get_file_info(file_path):
file_name = os.path.basename(file_path)
mod_time = datetime.datetime.fromtimestamp(os.path.getmtime(file_path)).strftime('%Y-%m-%d %H:%M:%S')
file_extension = os.path.splitext(file_name)[1]
size_mb = round(os.path.getsize(file_path) / (1024 * 1024), 2)
return file_name, mod_time, file_extension, size_mb
def scan_files(root_dir, output_file):
with open(output_file, 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(['File Name', 'Modification Time', 'File Extension', 'Size (MB)'])
for root, dirs, files in os.walk(root_dir):
for file in files:
file_path = os.path.join(root, file)
if not is_system_or_program_file(file_path):
file_info = get_file_info(file_path)
writer.writerow(file_info)
def is_system_or_program_file(file_path):
system_dirs = ['/bin', '/sbin', '/usr/bin', '/usr/sbin', '/usr/local/bin', '/usr/local/sbin',
'/System', '/Library', '/Applications', '/Windows', '/Program Files', '/Program Files (x86)']
for dir in system_dirs:
if file_path.startswith(dir):
return True
return False
def main():
root_dir = '/' # Start scanning from the root directory
output_file = 'file_info.csv' # Output CSV file name
if platform.system() == 'Windows':
root_dir = 'C:\\' # Start scanning from the C: drive on Windows
scan_files(root_dir, output_file)
print(f"File information saved to {output_file}")
if __name__ == '__main__':
main()
This script does the following:
The get_file_info function takes a file path and returns a tuple containing the file name, modification time, file extension, and size in MB.
The scan_files function recursively scans all files starting from the specified root_dir. It opens the output CSV file and writes the file information for each file that is not a system or program file.
The is_system_or_program_file function checks if a file path belongs to a system or program directory. It returns True if the file is a system or program file, and False otherwise.
The main function sets the root_dir based on the operating system. On Windows, it starts scanning from the C: drive, while on other systems, it starts from the root directory. It then calls the scan_files function to perform the file scanning and saves the file information to the specified output_file.
The script checks if it is being run as the main program and calls the main function.
Note: Running this script may require administrative privileges to access certain directories on the system. Also, scanning the entire file system may take a considerable amount of time, especially on large systems with many files.