-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpid.py
More file actions
57 lines (47 loc) · 1.27 KB
/
pid.py
File metadata and controls
57 lines (47 loc) · 1.27 KB
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
# -*- coding: utf-8 -*-
'''
Created on 2018-01-05 15:01
---------
@summary: 记录pid 在主函数中import pid即可
---------
@author: Boris
'''
import os
PID_PATH = './pid/'
def mkdir(path):
try:
os.makedirs(path)
except OSError as exc: # Python >2.5
pass
def write_file(filename, content, mode = 'w'):
'''
@summary: 写文件
---------
@param filename: 文件名(有路径)
@param content: 内容
@param mode: 模式 w/w+ (覆盖/追加)
---------
@result:
'''
directory = os.path.dirname(filename)
mkdir(directory)
with open(filename, mode, encoding = 'utf-8') as file:
file.writelines(content)
def get_filepath_filename_fileext(file_path):
'''
@summary: 获取文件路径 名称 后缀
---------
@param file_path: 文件的绝对路径
---------
@result:
'''
file_path, file_name = os.path.split(file_path)
shot_name, extension = os.path.splitext(file_name)
return file_path, shot_name, extension
def get_pid():
return os.getpid()
def record_pid(file_name):
pid = get_pid()
print('%s 进程pid为 %d '%(file_name, pid))
pid_file_name = get_filepath_filename_fileext(file_name)[1]
write_file(PID_PATH + pid_file_name + '.txt', str(pid))