Download all the files in the Headfirstpython website and extract the "Sketch.txt" in Chapter 3 as an example:
Create a new idle session, import the OS module first, and change the working directory to a folder that contains the file "Sketch.txt", such as C:\\python33\\headfirstpython\\chapter3
The code is as follows:
>>> Import OS
>>> OS.GETCWD () #查看当前工作目录
' C:\\python33 '
>>> os.chdir (' C:/python33/headfirstpython/chapter3 ') #切换包含数据文件的文件夹
>>> OS.GETCWD () #查看切换后的工作目录
' C:\\python33\\headfirstpython\\chapter3 '
Open the file "Sketch.txt" and read and display the first two lines:
The code is as follows:
>>> data=open (' Sketch.txt ')
>>> print (Data.readline (), end= ")
Man:is This is the right to a argument?
>>> print (Data.readline (), end= ")
Other man:i ' ve told you once.
Go back to the beginning of the file, use the For statement to process each line in the file, and finally close the file:
The code is as follows:
>>> data.seek (0) #使用seek () method back to file start position
>>> for Each_line in data:
Print (each_line,end= ")
Man:is This is the right to a argument?
Other man:i ' ve told you once.
Man:no you haven ' t!
Other Man:yes I has.
Man:when?
Other man:just now.
Man:no you didn ' t!
Other Man:yes I did!
Man:you didn ' t!
Other man:i ' m telling, I did!
Man:you did not!
Other Man:oh I ' m Sorry, are this a five minute argument, or the full half hour?
man:ah! (taking out his wallet and paying) Just the five minutes.
Other man:just the five minutes. Thank.
Other Man:anyway, I did.
Man:you most certainly did not!
Other Man:now let's get one thing quite clear:i most definitely told you!
Man:oh no you didn ' t!
Other Man:oh Yes I did!
Man:oh no you didn ' t!
Other Man:oh Yes I did!
Man:oh look, this isn ' t an argument!
(pause)
Other Man:yes it is!
Man:no it isn ' t!
(pause)
Man:it ' s just contradiction!
Other man:no it isn ' t!
Man:it is!
Other man:it are not!
Man:you just contradicted me!
Other man:no I didn ' t!
Man:you did!
Other Man:no No no!
Man:you did just then!
Other man:nonsense!
Man: (exasperated) Oh, this is futile!!
(pause)
Other man:no it isn ' t!
Man:yes it is!
>>> Data.close ()
After reading the file, save the different role corresponding data to the list man and other separately:
The code is as follows:
Import OS
Print (OS.GETCWD ())
Os.chdir (' C:\Python33\HeadFirstPython\chapter3 ')
Man=[] #定义列表man接收Man的内容
Other=[] #定义列表other接收Other man's content
Try
Data=open ("Sketch.txt")
For Each_line in data:
Try
(role, Line_spoken) =each_line.split (': ', 1)
Line_spoken=line_spoken.strip ()
If role== ' man ':
Man.append (Line_spoken)
Elif role== ' Other mans ':
Other.append (Line_spoken)
Except ValueError:
Pass
Data.close ()
Except IOError:
Print (' The datafile is missing! ')
Print (man)
Print (Other)
Tips:
When opening a disk file using the open () method, the default access mode is R, which means read and does not need to be specifically specified;
To open a file to finish writing, you need to specify the mode W, such as Data=open ("Sketch.txt", "w"), and empty the existing content if the file already exists;
To append to a file, you need to specify pattern a, which does not empty the existing content;
To open a file to complete the write and read, and not empty the existing content, you need to specify the mode w+;
For example, when you save the man and other content in the previous example as a file, you can modify the following:
The code is as follows:
Import OS
Print (OS.GETCWD ())
Os.chdir (' C:\Python33\HeadFirstPython\chapter3 ')
Man=[]
Other=[]
Try
Data=open ("Sketch.txt")
For Each_line in data:
Try
(role, Line_spoken) =each_line.split (': ', 1)
Line_spoken=line_spoken.strip ()
If role== ' man ':
Man.append (Line_spoken)
Elif role== ' Other mans ':
Other.append (Line_spoken)
Except ValueError:
Pass
Data.close ()
Except IOError:
Print (' The datafile is missing! ')
Try
Man_file=open (' Man.txt ', ' W ') #以w模式访问文件man. txt
Other_file=open (' Other.txt ', ' W ') #以w模式访问文件other. txt
Print (man, file=man_file) #将列表man的内容写到文件中
Print (Other, file=other_file)
Except IOError:
Print (' File error ')
Finally
Man_file.close ()
Other_file.close ()
But why does the 26th line print () cause an error? "Syntax error while detecting tuple", there is great God can give doubts