Download all the files from the HeadFirstPython website. decompress the package and use the unzip sketch.txt in chapter 3 as an example:
A new idlesession is introduced to the ossag first, and the job directory is replaced with a folder containing the file "sketch.txt", for example, C: \ Python33 \ HeadFirstPython \ chapter3
Copy codeThe Code is as follows:
>>> Import OS
>>> OS. getcwd () # view the current working directory
'C: \ python33'
>>> OS. chdir ('C:/Python33/HeadFirstPython/chapter3 ') # Switch the folder containing data files
>>> OS. getcwd () # view the working directory after switching
'C: \ Python33 \ HeadFirstPython \ chapter3'
Open the “sketch.txt file and read and display the first two lines:
Copy codeThe Code is as follows:
>>> Data‑open('sketch.txt ')
>>> Print (data. readline (), end = '')
Man: Is this the right room for an argument?
>>> Print (data. readline (), end = '')
Other Man: I 've told you once.
Return to the starting position of the file, use the for statement to process each line in the file, and close the file:
Copy codeThe Code is as follows:
>>> Data. seek (0) # Use the seek () method to return to the start position of the file
>>> For each_line in data:
Print (each_line, end = '')
Man: Is this the right room for an argument?
Other Man: I 've told you once.
Man: No you haven't!
Other Man: Yes I have.
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 you, I did!
Man: You did not!
Other Man: Oh I'm sorry, is 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 you.
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 is NOT!
Man: You just contradicted me!
Other Man: No I didn't!
Man: You DID!
Other Man: 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 corresponding data of different role to the list man and other respectively:
Copy codeThe Code is as follows:
Import OS
Print (OS. getcwd ())
OS. chdir ('C: \ Python33 \ HeadFirstPython \ chapter3 ')
Man = [] # define list man to receive Man's content
Other = [] # define the list of other receiving Other Man 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 = 'otherman ':
Other. append (line_spoken)
Failed t ValueError:
Pass
Data. close ()
Handle t IOError:
Print ('the datafile is missing! ')
Print (man)
Print (other)
Tips:
When you use the open () method to open a disk file, the default access mode is r, which indicates reading and does not need to be specified;
To open a file and complete writing, you must specify the mode w, such as data = open ("sketch.txt", "w"). If the file already exists, the existing content is cleared;
To append an object to a file, you must specify Mode a. The existing content is not cleared;
To open a file to complete writing and reading without clearing the existing content, you must specify the mode w +;
For example, when you save the man and other content in the previous example as a file, you can modify it as follows:
Copy codeThe 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 = 'otherman ':
Other. append (line_spoken)
Failed t ValueError:
Pass
Data. close ()
Handle t IOError:
Print ('the datafile is missing! ')
Try:
Man_file1_open('man.txt ', 'w') using the w‑format man.txt File
Other_file1_open('other.txt ', 'w') uses the "w" pattern to export the file "other.txt ".
Print (man, file = man_file) # Write the list man content to the file
Print (other, file = other_file)
Handle t IOError:
Print ('file error ')
Finally:
Man_file.close ()
Other_file.close ()
But why does 26th rows print () report an error? "Syntax error while detecting tuple", can you give it a try?