Python core programming, Version 2, 230th, page 9, chapter 9 exercises-answers to Python core programming-self-developed-

Source: Internet
Author: User

The answer in this blog is not from official resources, but from my own exercises, which may be incorrect.

9-1. file filtering. Displays all rows of a file, ignoring the rows starting with. This character is used as annotation symbols for most script files, such as Python, Perl, and Tcl. Additional question: handle comments that do not start with the first character.
[Answer]
(A) The Code is as follows:

fobj = open('c:\Python Test\P_1.txt')for eachLine in fobj:    if eachLine[0] != '#':        print eachLine,fobj.close()

The file p_1.txt is:
Apple
Banana
# Orange
Orange #
Orange # orange
Pie
Donut

[Execution result]
Apple
Banana
Orange #
Orange # orange
Pie
Donut

(B) the additional question code is as follows:

fobj = open('c:\Python Test\P_1.txt')for eachLine in fobj:    switch = True    for i in eachLine[1:]:        if i == '#': switch = False    if switch: print eachLine,fobj.close()

[Execution result]
Apple
Banana
# Orange
Pie
Donut

9-2. File Access. The system prompts you to enter numbers N and file F, and then displays the first N lines of the file.
[Answer]
The Code is as follows:

N = raw_input("Please input the line numbers: ... ")F = raw_input("Please input the full location and file name: ... ")fobj = open(F)k = fobj.readlines()for i in k[:int(N)]:    print i,fobj.close()

[Reference]
Http://www.czug.org/python/pyessentialref/09.htm

9-3. File Information, prompting you to enter a file name, and then display the total number of lines of the text file.
[Answer]
The Code is as follows:

Filename = raw_input ("Please input the file location and name :... ") fobj = open (filename) print len (fobj. readlines () fobj. close () ************ Example 9.1. OS & OS. path Modules Example (ospathex. py) [Example source code] import osfor tmpdir in ('/tmp', r 'C: \ Python test'): if OS. path. isdir (tmpdir): breakelse: print 'no temp directory available 'tmpdir = ''if tmpdir: OS. chdir (tmpdir) 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... 'fobj = open ('test', 'w') fobj. write ('foo \ n') fobj. write ('bar \ n') fobj. 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: 'fobj = open (path) for eachLine in fobj: print eachLine, fobj. 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'


[Execution result]
* ** Current temporary directory
C: \ Python Test
* ** Creating example directory...
* ** New working directory:
C: \ Python Test \ example
* ** Original directory listing:
[]
* ** Creating test file...
* ** Updated directory listing:
['Test']
* ** Renaming 'test' to 'filetest.txt'
* ** Updated directory listing:
Using 'filetest.txt ']
* ** Full file pathname
C: \ Python Test \ example \ filetest.txt
*** (Pathname, basename) =
('C: \ Python Test \ example ', 'filetest.txt ')
*** (Filename, extension) =
('Filetest', '.txt ')
* ** Displaying file contents:
Foo
Bar
* ** Deleting test file
* ** Updated directory listing:
[]
* ** Deleting test directory
* ** DONE

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.