Tutorialspoint

Python 3 Os Popen Method

# !/usr/bin/python3
import os, sys

# using command mkdir
a = 'mkdir nwdir'
b = os.popen(a,'r',1)

print (b)

Python 3 Os Pipe Method

#!/usr/bin/python3
import os, sys

print ("The child will write text to a pipe and ")
print ("the parent will read the text written by child...")

# file descriptors r, w for reading and writing
r, w = os.pipe() 

processid = os.fork()
if processid:
   # This is the parent process 
   # Closes file descriptor w
   os.close(w)
   r = os.fdopen(r)
   print ("Parent reading")
   str = r.read()
   print ("text =", str   )
   sys.exit(0)
else:
   # This is the child process
   os.close(r)
   w = os.fdopen(w, 'w')
   print ("Child writing")
   w.write("Text written by child...")
   w.close()
   print ("Child closing")
   sys.exit(0)

Python 3 Os Pathconf Method

#!/usr/bin/python3
import os, sys

print ("%s" % os.pathconf_names)

# Retrieve maximum length of a filename
no = os.pathconf('a2.py', 'PC_NAME_MAX')
print ("Maximum length of a filename :%d" % no)

# Retrieve file size
no = os.pathconf('a2.py', 'PC_FILESIZEBITS') 
print ("file size in bits  :%d" % no)

Python 3 Os Openpty Method

# !/usr/bin/python3
import os

# master for pty, slave for tty
m,s = os.openpty()

print (m)
print (s)

# showing terminal name
s = os.ttyname(s)
print (m)
print( s)

Python 3 Os Open Method

#!/usr/bin/python3
import os, sys

# Open a file
fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )

# Write one string

line = "this is test" 
# string needs to be converted byte object
b = str.encode(line)
os.write(fd, b)

# Close opened file
os.close( fd)

print ("Closed the file successfully!!")

Python 3 Os Makedirs Method

#!/usr/bin/python3
import os, sys

# Path to be created
path = "d:/tmp/home/monthly/daily"

os.makedirs( path, 493 ) #decimal equivalent of 0755 used on Windows

print ("Path is created")

Python 3 Os lstat Method

#!/usr/bin/python3
import os, sys

# Open a file
path = "d:\\python3\\foo.txt"
fd = os.open( path, os.O_RDWR|os.O_CREAT )

# Close opened file
os.close( fd )

# Now get  the touple
info = os.lstat(path)

print ("File Info :", info)

# Now get uid of the file
print ("UID of the file :%d" % info.st_uid)

# Now get gid of the file
print ("GID of the file :%d" % info.st_gid)

Python 3 Os lseek Method

#!/usr/bin/python3
import os, sys

# Open a file
fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )

# Write one string
line = "This is test"
b = line.encode()
os.write(fd, b)

# Now you can use fsync() method.
# Infact here you would not be able to see its effect.
os.fsync(fd)

# Now read this file from the beginning
os.lseek(fd, 0, 0)
line = os.read(fd, 100)
print ("Read String is : ", line.decode())

# Close opened file
os.close( fd )

print ("Closed the file successfully!!")

Python 3 Os Link Method

#!/usr/bin/python3
import os, sys

# Open a file
path = "d:\\python3\\foo.txt"
fd = os.open( path, os.O_RDWR|os.O_CREAT )

# Close opened file
os.close( fd )

# Now create another copy of the above file.
dst = "d:\\tmp\\foo.txt"
os.link( path, dst)

print ("Created hard link successfully!!")

Python 3 Os Isatty Method

#!/usr/bin/python3
import os, sys

# Open a file
fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )

# Write one string
line = "This is test"
b = line.encode()
os.write(fd, b)

# Now use isatty() to check the file.
ret = os.isatty(fd)

print ("Returned value is: ", ret)

# Close opened file
os.close( fd )

Advertisements
Loading...