To store screen output or any specific value
How to store screen output any specific value
There are two types of value:
1. Writing a specific value
2. Capturing output from screen
1. Writing a specific value:
- Screen _value is the string you want to store in the text file.
- file_path is the path to the text file where you want to store the value (output.txt in this case)
- open (file _path,’w’)opens the file output.txt in write mode (‘w’). this will create the file if it doesn’t exist. or truncate (clear) the file if it does exist.
- file.write(screen _value)writes the screen _value string to the file.
2.capturing output from screen:
- subprocess.run([‘1s’,’-1′],capture_output=true,text=true) executes the command 1s-1(list detailed directory contents in Unix)and captures it’s output (stdout).
- result.stdout contains the captured output as a strin is then written to output.txt.
- Subprocess.run([‘1s’,’-1′], capture_output=true,text=true) executes the command 1s-1(list detailed directory contents in unix)and captures it’s output (stdout).
How to store either a specific value or captured output:
- ensure you have necessary permission to write to the specified file path.
- handle errors and exception.expecially when dealing with file operations.
- adjust file paths and commands (sub process.run) based on your specific requirements.
- these methods demonstrate how to store either a specific value or captured output to a text file using python adjust the code according to what you need to store from your “screen”.
import tkinter as tk root=tk.TK() Screen _width= root.winfo_screen width() Screen _height= root.winfo_screenheight() root.destory() Output_file= "Screen_resolution.txt" with open (output_file,'w')as file: file.write(f"screen resolution: {screen_width}×{screen_height}\n") print(f"screen resolution ({screen_width}×{screen_height}) has been saved to {output_file}")
Output:
Screen resolution:1920×1080