异度部落格

学习是一种生活态度。

0%

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
'''
Created on 2011-8-1

@author: Killua
@E-mail:[email protected]
@Description:
'''

#!/usr/bin/env python3

import os
import time

#使用os模块进行文本替换
def context_replace(file, search_for, replace_with, new_file = 'new_file'):
try:
#remove old temp
os.remove(new_file)
except os.error:
pass

#open files
fi = open(file)
fo = open(new_file, 'w')

#replace context
for line in fi.readlines():
fo.write(line.replace(search_for, replace_with))

#close files
fi.close()
fo.close()

#使用 os 列出目录下的文件
def file_list(filepath):
for filename in os.listdir(filepath):
print(filename)

#使用 os 模块查看当前工作目录
def current_word_dir():
print("Currnet Directory:" + os.getcwd())

#使用 os 模块创建/删除目录
def make_dir(dir_name):
os.mkdir(dir_name)

def delete_dir(dir_name):
if not os.path.isdir(dir_name):
print("It's not a directory")
return
else:
if len(os.listdir(dir_name)) == 0:
os.rmdir(dir_name)
else:
print("The directory you want to delete is not empty,")

#使用 os 模块获取文件属性
def get_file_info(filename):
file_state = os.stat(filename)
mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime = file_state
print("size:", size, "bytes")
print("owner:", uid, gid)
print("created:", time.ctime(ctime))
print("last accessed:", time.ctime(atime))
print("last modified:", time.ctime(mtime))
print("mode:", oct(mode))
print("inode/dev", ino, dev)

#使用 os 执行操作系统命令
def os_command_excute(cmd):
os_name = os.name
if os_name == "nt":
print("Windows Command")
else:
print("Unix/Linux Command")
os.system(cmd)

#Test
def main():
#===Just For Test===
#context_replace("sample", 'a', 'A')
#file_list('/')
#current_word_dir()
#make_dir('sample_dir')
#delete_dir('sample_dir')
#get_file_info("sample")
os_command_excute("ls -l")

if __name__ == "__main__":
main()