编程札记

P

Python3 os 模块 Readlink 方法

Python3 os 模块 Readlink 方法 # !/usr/bin/python3 import os src = 'd://tmp//python3' dst = 'd://tmp//python2' # This creates a symbolic link on python in tmp directory os.symlink(src, dst) # Now let us use readlink to display the source of the link. path = os.readlink( dst ) print (path)

阅读全文 ↗
P

Python3 os 模块 Read 方法

Python3 os 模块 Read 方法 # !/usr/bin/python3 import os, sys # Open a file fd = os.open("foo.txt",os.O_RDWR) # Reading text ret = os.read(fd,12) print (ret.decode()) # Close opened file os.close(fd) print ("Closed the file successfully!!")

阅读全文 ↗
P

Python3 Range 函数

Python3 Range 函数 #!/usr/bin/python3 for letter in 'Python': # traversal of a string sequence print ('Current Letter :', letter) print() fruits = ['banana', 'apple', 'mango'] for fruit in fruits: # traversal of List sequence print ('Current fruit :', fruit) print ("Good bye!")

阅读全文 ↗
P

Python3 os 模块 Pipe 方法

Python3 os 模块 Pipe 方法 #!/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)

阅读全文 ↗
P

Python3 os 模块 Pathconf 方法

Python3 os 模块 Pathconf 方法 #!/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)

阅读全文 ↗
P

Python3 os 模块 Openpty 方法

Python3 os 模块 Openpty 方法 # !/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)

阅读全文 ↗
P

Python3 os 模块 Open 方法

Python3 os 模块 Open 方法 #!/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!!")

阅读全文 ↗
P

Python3 os 模块 Makedirs 方法

Python3 os 模块 Makedirs 方法 #!/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")

阅读全文 ↗
P

Python3 os 模块 lstat 方法

Python3 os 模块 lstat 方法 #!/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)

阅读全文 ↗
P

Python3 os 模块 lseek 方法

Python3 os 模块 lseek 方法 #!/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!!")

阅读全文 ↗