# 文件

Python
fin = open('filename','w')
fin.write('1234\n')
fin.close()
>>>import os
>>>cwd = os.getcwd()
>>>cwd
'C:\\Users\\hasee\\Desktop\\python\\.vscode'
>>> os.path.abspath('emma')
'C:\\Users\\hasee\\Desktop\\python\\.vscode\\emma'


14.4 查看文档

OS

os.walk(top, topdown=True, onerror=None, followlinks=False)

Generate the file names in a directory tree by walking the tree either top-down or bottom-up. For each directory in the tree rooted at directory top (including top itself), it yields a 3-tuple (dirpath, dirnames,filenames).

dirpath is a string, the path to the directory. dirnames is a list of the names of the subdirectories in dirpath(excluding '.' and '..'). filenames is a list of the names of the non-directory files in dirpath. Note that the names in the lists contain no path components. To get a full path (which begins with top) to a file or directory in dirpath, do os.path.join(dirpath, name).

If optional argument topdown is True or not specified, the triple for a directory is generated before the triples for any of its subdirectories (directories are generated top-down). If topdown is False, the triple for a directory is generated after the triples for all of its subdirectories (directories are generated bottom-up). No matter the value of topdown, the list of subdirectories is retrieved before the tuples for the directory and its subdirectories are generated.

When topdown is True, the caller can modify the dirnames list in-place (perhaps using del or slice assignment), and walk() will only recurse into the subdirectories whose names remain in dirnames; this can be used to prune the search, impose a specific order of visiting, or even to inform walk() about directories the caller creates or renames before it resumes walk() again. Modifying dirnames when topdown is Falsehas no effect on the behavior of the walk, because in bottom-up mode the directories in dirnames are generated before dirpath itself is generated.

By default, errors from the scandir() call are ignored. If optional argument onerror is specified, it should be a function; it will be called with one argument, an OSError instance. It can report the error to continue with the walk, or raise the exception to abort the walk. Note that the filename is available as the filename attribute of the exception object.

By default, walk() will not walk down into symbolic links that resolve to directories. Set followlinks to True to visit directories pointed to by symlinks, on systems that support them.

Be aware that setting followlinks to True can lead to infinite recursion if a link points to a parent directory of itself. walk() does not keep track of the directories it visited already.

If you pass a relative pathname, don't change the current working directory between resumptions of walk(). walk() never changes the current directory, and assumes that its caller doesn't either.

This example displays the number of bytes taken by non-directory files in each directory under the starting directory, except that it doesn't look under any CVS subdirectory:

Python
import os
from os.path import join, getsize
for root, dirs, files in os.walk('python/Lib/email'):
print(root, "consumes", end=" ")
print(sum(getsize(join(root, name)) for name in files), end=" ")
print("bytes in", len(files), "non-directory files")
if 'CVS' in dirs:
dirs.remove('CVS')  # don't visit CVS directories


In the next example (simple implementation of shutil.rmtree()), walking the tree bottom-up is essential, rmdir() doesn't allow deleting a directory before the directory is empty:

Python
# Delete everything reachable from the directory named in "top",
# assuming there are no symbolic links.
# CAUTION:  This is dangerous!  For example, if top == '/', it
# could delete all your disk files.
import os
for root, dirs, files in os.walk(top, topdown=False):
for name in files:
os.remove(os.path.join(root, name))
for name in dirs:
os.rmdir(os.path.join(root, name))


Python
import os

def walk2(dirname):
"""Prints the names of all files in dirname and its subdirectories.

This is the exercise solution, which uses os.walk.

dirname: string name of directory
"""
for root, dirs, files in os.walk(dirname):
for filename in files:
print(os.path.join(root, filename))


14.5

Python
try:
fin = open('emma.txt')
except:
print('something went worong')


14-1

Python
def sed(str1, str2, filename1, filename2):
"""练习的说明有些问题，这个程序其实就是把固定的字符串换成想要目标字符串，然后把文件1的内容换到文件2去

str1: 想要换掉的str
str2: 目标str
filename1: 源文件
filename2: 目标文件"""
try:
fin1 = open(filename1)
fin2 = open(filename2,'w')
for line in fin1:
line = line.replace(str1, str2)
fin2.write(line)
fin1.close()
fin2.close()
print('done')
except:
print('something went worong')

sed('the', 'a', '1.txt', '2.txt')


14-2

Python
import dbm
import pickle

def signature(s):
"""Returns the signature of this string.

Signature is a string that contains all of the letters in order.

s: string
"""
# TODO: rewrite using sorted()
t = list(s)
t.sort()
t = ''.join(t)
return t

def all_anagrams(filename):
"""Finds all anagrams in a list of words.

filename: string filename of the word list

Returns: a map from each word to a list of its anagrams.
"""
d = {}
for line in open(filename):
word = line.strip().lower()
t = signature(word)

# TODO: rewrite using defaultdict
if t not in d:
d[t] = [word]
else:
d[t].append(word)
return d

def store_anagrams(d):
db = dbm.open('shelf', 'c')
for key in d:
db[key] = pickle.dumps(d[key])
db.close()

db = dbm.open('shelf')

# store_anagrams(all_anagrams('words.txt'))



14-3

Python
import os

def walk(dirname):
"""Finds the names of all files in dirname and its subdirectories.

dirname: string name of directory
"""
names = []
if '__pycache__' in dirname:
return names

for name in os.listdir(dirname):
path = os.path.join(dirname, name)

if os.path.isfile(path):
names.append(path)
else:
names.extend(walk(path))
return names

def compute_checksum(filename):
"""Computes the MD5 checksum of the contents of a file.

filename: string
"""
cmd = 'md5sum ' + filename
return pipe(cmd)

def check_diff(name1, name2):
"""Computes the difference between the contents of two files.

name1, name2: string filenames
"""
cmd = 'diff %s %s' % (name1, name2)
return pipe(cmd)

def pipe(cmd):
"""Runs a command in a subprocess.

cmd: string Unix command

Returns (res, stat), the output of the subprocess and the exit status.
"""
# Note: os.popen is deprecated
# now, which means we are supposed to stop using it and start using
# the subprocess module.  But for simple cases, I find
# subprocess more complicated than necessary.  So I am going
# to keep using os.popen until they take it away.

fp = os.popen(cmd)
stat = fp.close()
assert stat is None
return res, stat

def compute_checksums(dirname, suffix):
"""Computes checksums for all files with the given suffix.

dirname: string name of directory to search
suffix: string suffix to match

Returns: map from checksum to list of files with that checksum
"""
names = walk(dirname)

d = {}
for name in names:
if name.endswith(suffix):
res, stat = compute_checksum(name)
checksum, _ = res.split()

if checksum in d:
d[checksum].append(name)
else:
d[checksum] = [name]

return d

def check_pairs(names):
"""Checks whether any in a list of files differs from the others.

names: list of string filenames
"""
for name1 in names:
for name2 in names:
if name1 < name2:
res, stat = check_diff(name1, name2)
if res:
return False
return True

def print_duplicates(d):
"""Checks for duplicate files.

Reports any files with the same checksum and checks whether they
are, in fact, identical.

d: map from checksum to list of files with that checksum
"""
for key, names in d.items():
if len(names) > 1:
print('The following files have the same checksum:')
for name in names:
print(name)

if check_pairs(names):
print('And they are identical.')

if __name__ == '__main__':
d = compute_checksums(dirname='.', suffix='.py')
print_duplicates(d)