1. The file is just a sequential sequence of bytes open () built-in function is the key to open the door of the file File_obj=open (file_name,access_mode= ' r/w/a, ' Buffering=-1) file_name is the file name to open, Can be a relative path or an absolute path. Assess_mode represents the mode of opening a file, R is read-only, w means write, and a means append. There is also a U-mode that represents universal wrap support for files that use R and U must exist, the open file with W mode is first emptied and then recreated. The data written by the ' a ' pattern is appended to the end of the Ask price. There is also ' + ' for readable writable, ' B ' for binary access buffering to indicate the buffer used to access the file: 0 for non-buffering, 1 for buffering only one row, any other value greater than 1 means using the given value as the size of the buffer 2, the file's built-in function: (1), Input: The Read () method is used to read bytes directly into a string, up to a given number of bytes. If no size is given, it is read to the end. The ReadLine () method reads a line of open files and then outputs the entire line (including the line terminator). ReadLines (): reads all rows (remaining rows) and then returns them as a list of strings (2) The output write () method Writelines () method is for the operation of the list, takes a string as a parameter, Put them in the file core Note: Preserve line separators: When reading rows from a file using read and readlines, Python does not delete the line delimiter, so this operation is left to the programmer; Common program: F=open (' My.txt ', ' R ') data=[ Line.strip () for line in F.readlines ()]f.close () Similarly, write and Writelines () should also have the user handle (3) The move within the file: the Seek () method. The text () method. (4) file iteration: For eachline I f: .... (5) Close () method closes access to a file note: The Print statement defaults to the end of the output with a newline character, and a comma will solve the problem. 3. Command line arguments: is a parameter other than the program name when calling a program. The SYS module accesses command-line parameters through the SYS.ARGV property. SYS.ARGV is the list of command-line arguments Len (SYS.ARGV) is the number of command-line arguments (ARGC) 4, File system: Access to the filesystem is mostly implemented through Python's OS modules. This module is the primary interface for Python to access the functionality of the operating system. Examples: OS and Os.patExamples of H modules
ImportOS forTempDirinch('/ tmp'R'C:\Temp'): ifOs.path.isdir (tempdir): BreakElse: Print('No temp directory available') TempDir="'ifTempdir:os.chdir (tempdir) CWD=OS.GETCWD ()Print('****current temporary directory:') Print(CWD)Print('****creating Example Directory:') Os.mkdir ('Example') Os.chdir ('Example') CWD=OS.GETCWD ()Print('****new working directory:') Print(CWD)Print("****original Directory Listing:") Print(Os.listdir (CWD))Print('****creating test File ...') F=open ('Test','W') F.write ('foo\n') F.write ('bar\n') F.close ()Print('****updated Directory Listing:') Print(Os.listdir (CWD))Print("****renaming ' test ' to ' Filetest.txt '") Os.rename ('Test','filetest. txt') Print('****updated Directory Listing:') Print(Os.listdir (CWD)) path=Os.path.join (Cwd,os.listdir (CWD) [0])Print('****full File Pathname:') Print(PATH)Print('* * * (pathname,basename) = =') Print(Os.path.split (path))Print('* * * (filename,extension) = =') Print(Os.path.splitext (os.path.basename (Path )))Print('****displaying file Contents:') F=Open (Path) forEachlineinchF:Print(Eachline) f.close ()Print("****deleting test File") os.remove (path)Print('****updated Directory Listing:') Print(Os.listdir (CWD)) Os.chdir (Os.pardir)Print('****deleting Test Directory') Os.rmdir ('Example') Print('****done')
Python Learning notes--documents