0% found this document useful (0 votes)
5 views7 pages

Os Module Advance

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views7 pages

Os Module Advance

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

import os

# os.rmdir('gannoba_module')

#so this apparantly reads the data in a txt file


f = open("demo.txt", "r") # Open for reading
data = f.read()
print(data)
f.close()

fuck off

# this sucker overrights the previous file so this is sussy to use


with open("demo.txt", "w") as f:
f.write("This is a line of text.now where is the line before this\
n")

f = open("demo.txt", "r") # Open for reading


data = f.read()
print(data)
f.close()

This is a line of text.now where is the line before this

# import os
print(os.cpu_count())
#tells cpu

# import os
# checks for file access and permissiions and if it exists returns ok

if os.access("demo.txt", os.F_OK):
print("File exists")

File exists

# import os
# will check what is enviroment variables
print(os.environ['PATH'])

c:\Users\prasa\AppData\Local\Programs\Python\Python313;c:\Users\prasa\
AppData\Roaming\Python\Python313\Scripts;C:\Windows\system32;C:\
Windows;C:\Windows\System32\Wbem;C:\Windows\System32\
WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files\
NVIDIA Corporation\NVIDIA NvDLISR;C:\WINDOWS\system32;C:\WINDOWS;C:\
WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\
WINDOWS\System32\OpenSSH\;C:\Users\prasa\AppData\Local\Programs\
Python\Python313\Scripts\;C:\Users\prasa\AppData\Local\Programs\
Python\Python313\;C:\Users\prasa\AppData\Local\Programs\Python\
Launcher\;C:\Users\prasa\AppData\Local\Microsoft\WindowsApps;C:\Users\
prasa\AppData\Local\Programs\Microsoft VS Code\bin;C:\Windows\
system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\
WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files\
NVIDIA Corporation\NVIDIA NvDLISR;C:\WINDOWS\system32;C:\WINDOWS;C:\
WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\
WINDOWS\System32\OpenSSH\;C:\Users\prasa\AppData\Local\Programs\
Python\Python313\Scripts\;C:\Users\prasa\AppData\Local\Programs\
Python\Python313\;C:\Users\prasa\AppData\Local\Programs\Python\
Launcher\;C:\Users\prasa\AppData\Local\Microsoft\WindowsApps;C:\Users\
prasa\AppData\Local\Programs\Microsoft VS Code\bin

# import os
print(os.pardir) # Output: '..'
#represents the parent directory

..

# O_SHORT_LIVED find out how to use this process ,O_TEMPORARY,


O_TEXT,_Environ and what do you mean by depreciated ,
__builtins__,_exists,error, kill,lchmod will check what is enviroment
variables
print(os.environ['PATH'])
print(os.pardir) # Output: '..'
#represents the parent directory
print(os.getpid())

with open("demo.txt", "a") as f: # 'a' is equivalent to 'w' +


O_APPEND
f.write("Appended text\n")
# append sinfo how find out

import os
os.chmod("demo.txt", 0o777) # Set permissions to read, write, and
execute for all

import os
print(os.getlogin())

prasa

import os
print(os.getpid())

3472

import os

# Open a file with the O_SHORT_LIVED flag


fd = os.open("demo.txt",os.O_SHORT_LIVED)
print(fd)
# ... perform file operations ...
os.close(fd)

try:
with open("nonexistent_file.txt", "r") as f:
pass
except OSError as e:
print(f"Error: {e}")

Error: [Errno 2] No such file or directory: 'nonexistent_file.txt'

# this is the answer given to me by gemini so recheck it befor


finalizing and that info is avaliable at the file drop coloumn in
pandas in the gemini workspace

Certainly, let's break down those specific os module elements:

1. O_SHORT_LIVED

Purpose: (Windows-specific) This flag is a hint to the operating


system that the file will be open for a short duration.

How to Use:
You typically use O_SHORT_LIVED in conjunction with other
flags when opening a file using the os.open() function.
Example:
Python

import os

# Open a file with the O_SHORT_LIVED flag


fd = os.open("tempfile.txt", os.O_CREAT | os.O_WRONLY |
os.O_SHORT_LIVED)
# ... perform file operations ...
os.close(fd)

Key Points:
This flag can potentially optimize file handling by allowing
the operating system to allocate resources more efficiently for short-
lived files.
The behavior of O_SHORT_LIVED may vary depending on the
specific Windows version.

2. O_TEMPORARY

Purpose: (Windows-specific) This flag is also a hint to the


operating system that the file is temporary.
How to Use: Similar to O_SHORT_LIVED, use it with other flags when
opening a file:
Python

import os

fd = os.open("tempfile.txt", os.O_CREAT | os.O_WRONLY |


os.O_TEMPORARY)
# ... perform file operations ...
os.close(fd)

Key Points:
This flag helps the operating system manage temporary files
more effectively, potentially improving performance and resource
utilization.

3. O_TEXT

Purpose: (Windows-specific) This flag opens a file in text mode.


How to Use:
Python

with open("myfile.txt", "w", encoding="utf-8") as f:


f.write("This is text data.")

Key Points:
In text mode, the operating system handles line endings (e.g.,
converting between '\n' and '\r\n') appropriately for the current
platform.
It's generally recommended to use the encoding parameter with
open() for better cross-platform compatibility.

4. _Environ

Purpose: (Deprecated) This was an internal variable representing


the environment variables.
Key Points:
Deprecated: This is no longer recommended. Use os.environ
instead.
os.environ is a dictionary that provides access to the
environment variables.

5. __builtins__

Purpose: A dictionary containing built-in functions and names


(like print, len, int, etc.) available in the current Python scope.
Key Points: You can explore the contents of __builtins__ to see
the available built-in functions and objects in your Python
environment.

6. _exists

Purpose: (Internal function) Checks if a path exists.


Key Points: This is an internal function and not typically used
directly. Use os.path.exists(path) instead.

7. error

Purpose: The OSError class represents operating system-related


errors.
Key Points: This class is raised when operating system functions
encounter issues (e.g., file not found, permission denied).
Example:
Python

try:
with open("nonexistent_file.txt", "r") as f:
pass
except OSError as e:
print(f"Error: {e}")

8. kill

Purpose: Sends a signal to a process.


Key Points:
Caution: Using os.kill() requires careful consideration and
appropriate permissions.
Example: (This is for demonstration purposes only; use with
extreme caution)
Python

import os
# Replace with the actual process ID
os.kill(12345, 9) # Send the SIGKILL signal (terminate the
process)

9. lchmod
Purpose: (Unix-specific) Changes the permissions of a file,
following symlinks.
Key Points:
If the path is a symbolic link, lchmod() modifies the
permissions of the actual file pointed to by the link, not the link
itself.

10. os.environ['PATH']

Purpose: Accesses the PATH environment variable, which specifies


the directories where the operating system searches for executable
files.
Example:
Python

import os
print(os.environ['PATH'])

11. os.pardir

Purpose: Represents the parent directory (usually "..").


Example: (see explanation above)

12. os.getpid()

Purpose: Gets the unique identifier (process ID) of the current


Python process.
Example:
Python

import os
print(f"Current process ID: {os.getpid()}")

Key Points:
You can use this to identify and manage your own processes.
It's often useful for debugging and logging.

I hope this comprehensive explanation clarifies the functionality of


these os module elements!

# ONCE THIS IS DONE THE OS MODULE IS COMPLETLY EXTRACTED NOW THIS CAN
BE SAFETLY WRITTEN AND THIS MODULE CAN BE UTILIZED

You might also like