forked from ripfire-web/brand-new-DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspaceOccupied.py
More file actions
43 lines (29 loc) · 1.69 KB
/
Copy pathspaceOccupied.py
File metadata and controls
43 lines (29 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# To measure the space taken by a Python file while it's running, you can use the `psutil` library. `psutil` provides information on system utilization, including memory and disk usage. You can monitor the disk usage before and after running your Python file to determine the space used. Here's how you can do it:
# First, you need to install the `psutil` library if you haven't already:
# bash #
# pip install psutil
# Then, create a Python program to measure the space taken by the Python file:
import os
import psutil
def get_file_size(file_path):
# Get the size of the file in bytes
file_size = os.path.getsize(file_path)
return file_size
if __name__ == "__main__":
print("File Size Measurement")
print("Enter the path to the Python file you want to measure:")
# Get the file path from the user
file_to_measure = input()
if os.path.isfile(file_to_measure):
# Get the disk usage before running the Python file
disk_usage_before = psutil.disk_usage('/')
# Run the Python file
os.system(f'python {file_to_measure}')
# Get the disk usage after running the Python file
disk_usage_after = psutil.disk_usage('/')
# Calculate the space used during execution
space_used = disk_usage_after.used - disk_usage_before.used
print(f"The file '{file_to_measure}' used {space_used} bytes of space during execution.")
else:
print(f"The file '{file_to_measure}' does not exist or is not a valid file.")
# This program prompts the user for the path to the Python file, runs the file, and then calculates the disk space used before and after execution to determine the space used during the program's runtime.