编程札记

P

Python3 os 模块 Link 方法

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

阅读全文 ↗
P

Python3 os 模块 Isatty 方法

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

阅读全文 ↗
P

Python3 while else 循环

Python3 while else 循环 #!/usr/bin/python3 count = 0 while count < 5: print (count, " is less than 5") count = count + 1 else: print (count, " is not less than 5")

阅读全文 ↗
P

Python3 os 模块 Getcwdu 方法

Python3 os 模块 Getcwdu 方法 #!/usr/bin/python3 import os, sys # First go to the "/var/www/html" directory os.chdir("/var/www/html" ) # Print current working directory print ("Current working dir : %s" % os.getcwdu()) # Now open a directory "/tmp" fd = os.open( "/tmp", os.O_RDONLY ) # Use os.fchdir() method to change the dir os.fchdir(fd) # Print current working directory print ("Current working dir : %s" % os.getcwdu()) # Close opened directory. os.close( fd )

阅读全文 ↗
P

Python3 os 模块 Getcwd 方法

Python3 os 模块 Getcwd 方法 #!/usr/bin/python3 import os, sys # First go to the "/var/www/html" directory os.chdir("/var/www/html" ) # Print current working directory print ("Current working dir : %s" % os.getcwd()) # Now open a directory "/tmp" fd = os.open( "/tmp", os.O_RDONLY ) # Use os.fchdir() method to change the dir os.fchdir(fd) # Print current working directory print ("Current working dir : %s" % os.getcwd()) # Close opened directory. os.close( fd )

阅读全文 ↗
P

Python3 os 模块 Ftruncate 方法

Python3 os 模块 Ftruncate 方法 #!/usr/bin/python3 import os, sys # Open a file fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT ) # Write one string os.write(fd, "This is test - This is test") # Now you can use ftruncate() method. os.ftruncate(fd, 10) # Now read this file from the beginning. os.lseek(fd, 0, 0) str = os.read(fd, 100) print ("Read String is : ", str) # Close opened file os.close( fd ) print ("Closed the file successfully!!")

阅读全文 ↗
P

Python3 os 模块 Fsync 方法

Python3 os 模块 Fsync 方法 #!/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) b = line.decode() print ("Read String is : ", b) # Close opened file os.close( fd ) print ("Closed the file successfully!!")

阅读全文 ↗
P

Python3 os 模块 Fstat 方法

Python3 os 模块 Fstat 方法 #!/usr/bin/python3 import os, sys # Open a file fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT ) # Now get the touple info = os.fstat(fd) 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) # Close opened file os.close( fd)

阅读全文 ↗
P

Python3 os 模块 Fpathconf 方法

Python3 os 模块 Fpathconf 方法 #!/usr/bin/python3 import os, sys # Open a file fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT ) print ("%s" % os.pathconf_names) # Now get maximum number of links to the file. no = os.fpathconf(fd, 'PC_LINK_MAX') print ("Maximum number of links to the file. :%d" % no) # Now get maximum length of a filename no = os.fpathconf(fd, 'PC_NAME_MAX') print ("Maximum length of a filename :%d" % no) # Close opened file os.close( fd) print ("Closed the file successfully!!")

阅读全文 ↗
P

Python3 os 模块 Fdopen 方法

Python3 os 模块 Fdopen 方法 #!/usr/bin/python3 import os, sys # Open a file fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT ) # Now get a file object for the above file. fo = os.fdopen(fd, "w+") # Tell the current position print ("Current I/O pointer position :%d" % fo.tell()) # Write one string fo.write( "Python is a great language.\nYeah its great!!\n"); # Now read this file from the beginning. os.lseek(fd, 0, 0) str = os.read(fd, 100) print ("Read String is : ", str) # Tell the current position print ("Current I/O pointer position :%d" % fo.tell()) # Close opened file fo.close() print ("Closed the file successfully!!")

阅读全文 ↗
P

Python3 os 模块 Fchdir 方法

Python3 os 模块 Fchdir 方法 #!/usr/bin/python3 import os, sys # First go to the "/var/www/html" directory os.chdir("/var/www/html" ) # Print current working directory print ("Current working dir : %s" % os.getcwd()) # Now open a directory "/tmp" fd = os.open( "/tmp", os.O_RDONLY ) # Use os.fchdir() method to change the dir os.fchdir(fd) # Print current working directory print ("Current working dir : %s" % os.getcwd()) # Close opened directory. os.close( fd )

阅读全文 ↗
P

Python3 os 模块 Dup2 方法

Python3 os 模块 Dup2 方法 #!/usr/bin/python3 import os, sys # Open a file fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT ) # Write one string using duplicate fd line = "this is test" # string needs to be converted byte object b = str.encode(line) os.write(fd, b) # Now duplicate this file descriptor as 1000 fd2 = 1000 os.dup2(fd, fd2); # Now read this file from the beginning using fd2. os.lseek(fd2, 0, 0) line = os.read(fd2, 100) str = line.decode() print ("Read String is : ", str) # Close opened file os.closerange( fd,fd2 ) print ("Closed the file successfully!!")

阅读全文 ↗