编程札记

P

Python3 os 模块 Dup 方法

Python3 os 模块 Dup 方法 #!/usr/bin/python3 import os, sys # Open a file fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT ) # Get one duplicate file descriptor d_fd = os.dup( fd ) # Write one string using duplicate fd line = "this is test" # string needs to be converted byte object b = str.encode(line) os.write(d_fd, b) # Close a single opened file os.closerange( fd, d_fd) print ("Closed all the files successfully!!")

阅读全文 ↗
P

Python3 os 模块 Dup 方法

Python3 os 模块 Dup 方法 #!/usr/bin/python3 import os, sys # Open a file fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT ) # Get one duplicate file descriptor d_fd = os.dup( fd ) # Write one string using duplicate fd line = "this is test" # string needs to be converted byte object b = str.encode(line) os.write(d_fd, b) # Close a single opened file os.closerange( fd, d_fd) print "Closed all the files successfully!!"

阅读全文 ↗
P

Python3 os 模块 Closerange 方法

Python3 os 模块 Closerange 方法 #!/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 a single opened file os.closerange( fd, fd) print ("Closed all the files successfully!!")

阅读全文 ↗
P

Python3 os 模块 Close 方法

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

Python3 os 模块 Access 方法 #!/usr/bin/python3 import os, sys # Assuming /tmp/foo.txt exists and has read/write permissions. ret = os.access("/tmp/foo.txt", os.F_OK) print ("F_OK - return value %s"% ret) ret = os.access("/tmp/foo.txt", os.R_OK) print ("R_OK - return value %s"% ret) ret = os.access("/tmp/foo.txt", os.W_OK) print ("W_OK - return value %s"% ret) ret = os.access("/tmp/foo.txt", os.X_OK) print ("X_OK - return value %s"% ret)

阅读全文 ↗
P

Python3 文件File Truncate 方法

Python3 文件File Truncate 方法 #!/usr/bin/python3 fo = open("foo.txt", "r+") print ("Name of the file: ", fo.name) line = fo.readline() print ("Read Line: %s" % (line)) fo.truncate() line = fo.readlines() print ("Read Line: %s" % (line)) # Close opened file fo.close()

阅读全文 ↗
P

Python3 文件File Tell 方法

Python3 文件File Tell 方法 #!/usr/bin/python3 fo = open("foo.txt", "r+") print ("Name of the file: ", fo.name) line = fo.readline() print ("Read Line: %s" % (line)) pos=fo.tell() print ("current position : ",pos) # Close opened file fo.close()

阅读全文 ↗
P

Python3 文件File Seek 方法

Python3 文件File Seek 方法 #!/usr/bin/python3 # Open a file fo = open("foo.txt", "r+") print ("Name of the file: ", fo.name) line = fo.readlines() print ("Read Line: %s" % (line)) # Again set the pointer to the beginning fo.seek(0, 0) line = fo.readline() print ("Read Line: %s" % (line)) # Close opened file fo.close()

阅读全文 ↗
P

Python3 文件File Readline 方法

Python3 文件File Readline 方法 #!/usr/bin/python3 # Open a file fo = open("foo.txt", "r+") print ("Name of the file: ", fo.name) line = fo.readline() print ("Read Line: %s" % (line)) line = fo.readline(5) print ("Read Line: %s" % (line)) # Close opened file fo.close()

阅读全文 ↗
P

Python3 文件File Readlines 方法

Python3 文件File Readlines 方法 #!/usr/bin/python3 # Open a file fo = open("foo.txt", "r+") print ("Name of the file: ", fo.name) line = fo.readlines() print ("Read Line: %s" % (line)) line = fo.readlines(2) print ("Read Line: %s" % (line)) # Close opened file fo.close()

阅读全文 ↗
P

Python3 文件File Read 方法

Python3 文件File Read 方法 #!/usr/bin/python3 # Open a file fo = open("foo.txt", "r+") print ("Name of the file: ", fo.name) line = fo.read(10) print ("Read Line: %s" % (line)) # Close opened file fo.close()

阅读全文 ↗