[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版
>>> objfile = open(fdfile, 'r', encoding='latin1', closefd=False) >>> objfile.read() 'Jello stdio file\nHello ...
>>> open('spam.txt', 'w').write('Hello stat world\n') # +1 for \r added 17 >>> import os >>> info = o ...
that exists now or in the future to all of the lines in a text file. If we code this module and put it in a directory on the mod ...
The scanner could similarly be improved. As a rule of thumb, we can also usually speed things up by shifting processing from Pyt ...
Notice that the newer context managers feature discussed earlier could save us a few lines here in the file-based filter of Exam ...
commonly used today. If you simply copy your script to any machine you wish to use it on, it will work regardless of which other ...
we’ve seen, scripts can grab a listing of file and directory names at this level by simply spawning the appropriate platform-spe ...
>>> >>> list(os.popen(r'c:\cygwin\bin\ls parts/part*')) ['parts/part0001\n', 'parts/part0002\n', 'parts/part00 ...
PP3E\Examples\PP3E\Lang\summer-alt.py PP3E\Examples\PP3E\Lang\summer.py PP3E\Examples\PP3E\PyTools\search_all.py Here, we get ba ...
Splitting and joining listing results In the last example, I pointed out that glob returns names with directory paths, whereas l ...
Luckily, in Python it’s almost as easy to process a directory tree as it is to inspect a single directory. We can either write a ...
Example 4-4. PP4E\System\Filetools\lister_walk.py "list file tree with os.walk" import sys, os def lister(root): # for a root di ...
This code loops through all the files at each level, looking for files with .py at the end of their names and which contain the ...
Example 4-5. PP4E\System\Filetools\lister_recur.py list files in dir tree by recursion import sys, os def mylister(currdir): pri ...
Technically, because filenames may contain arbitrary text, the os.listdir works in two modes in 3.X: given a bytes argument, thi ...
This might be especially useful on systems that use simple encodings such as ASCII or Latin-1, but may contain files with arbitr ...
As a rule of thumb, you should try to always provide an encoding name for text content if it might be outside the platform defau ...
...
CHAPTER 5 Parallel System Tools “Telling the Monkeys What to Do” Most computers spend a lot of time doing nothing. If you start ...
download task proceeds, the program as a whole shouldn’t stall—it still must respond to screen redraws, mouse clicks, and so on. ...
«
6
7
8
9
10
11
12
13
14
15
»
Free download pdf