学习自《python编程快速上手——让繁琐的工作自动化》
os基本操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| import os
os.path.join('usr', 'bin', 'spam')
os.getcwd()
os.chdir()
os.makedirs()
os.path.abspath(path)
os.path.isabs(path)
os.paath.relpath(path, start)
os.path.dirname(path)
os.path.basename(path)
os.path.split(path)
os.path.sep
path.split(os.path.sep)
os.path.getsize(path)
os.listdir()
os.path.exists(path)
os.path.isfile(path)
os.path.isdir(path)
|
读写一般文件基本操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
open(path, arg)
file.read()
file.readlines()
file.write(string)
file.close()
|
shelve基本操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| import shelve
file = shelve.open(path)
file['key'] = 'value'
file.close()
list(file.keys()) list(file.values())
|
其它
- 路径分隔:windows使用反斜杠
\
,osx和Linux使用斜杠/
。
- 大小写:windows和osx不区分大小写,Linux区分大小写。
- 相对路径和绝对路径,略。
.
表示当前文件夹..
表示父文件夹