Download our e-book of Introduction To Python
Sulochana Kamshetty
2 years ago
Import os
Print(os.name)
Output :- nt
fd = "AIML.txt"
popen() is similar to open()
import os
file = open(fd, 'w')
file.write("Hello")
file.close()
file = open(fd, 'r')
text = file.read()
print(text)
# popen()
provides a pipe/gateway and accesses the file directly
file = os.popen(fd, 'w')
file.write("insideaiml")
Hello
import os
fd = "AIML.txt"
file = open(fd, 'r')
text = file.read()
print(text)
os.close(file)
Output:
Traceback (most recent call last):
File "C:\Users\system\Desktop\insideaiml
content.py", line 6, in
os.close(file)
import os
fd = "AIML.txt"
os.rename(fd, insideaiml.txt')
Output: new text document with insideaiml will be created in place of aiml.txt
File "C:\Users\smithika\Desktop\insideaiml.
contentpy", line 3, in
os.rename(fd,'New.txt')
FileNotFoundError: [WinError 2] The system cannot find the
file specified: AIML.txt' -> 'New.txt'
import sys
sys.argv : A list of
strings which is the command line used to execute the script separated by
spaces.
sys.exit() : Exit Python
cleanly
sys.version : The Python
version string
import sys
x = 42
def my_display(x):
sys.displayhook = my_display
print x
output = 42