271 lines
No EOL
6.9 KiB
HTML
271 lines
No EOL
6.9 KiB
HTML
<!DOCTYPE html>
|
||
<html>
|
||
<head>
|
||
<meta charset="utf-8">
|
||
<meta name="tool" content="leanote-desktop-app">
|
||
<title>偷懒-写python脚本时自动添加文件头信息</title>
|
||
<style>
|
||
|
||
*{font-family:"lucida grande","lucida sans unicode",lucida,helvetica,"Hiragino Sans GB","Microsoft YaHei","WenQuanYi Micro Hei",sans-serif;}
|
||
|
||
body {
|
||
margin: 0;
|
||
}
|
||
|
||
/*公用文字样式*/
|
||
h1{font-size:30px}h2{font-size:24px}h3{font-size:18px}h4{font-size:14px}
|
||
.note-container{
|
||
width:850px;
|
||
margin:auto;
|
||
padding: 10px 20px;
|
||
box-shadow: 1px 1px 10px #eee;
|
||
}
|
||
#title {
|
||
margin: 0;
|
||
}
|
||
table {
|
||
margin-bottom: 16px;
|
||
border-collapse: collapse;
|
||
}
|
||
table th, table td {
|
||
padding: 6px 13px;
|
||
border: 1px solid #ddd;
|
||
}
|
||
table th {
|
||
font-weight: bold;
|
||
}
|
||
|
||
table tr {
|
||
background-color: none;
|
||
border-top: 1px solid #ccc;
|
||
}
|
||
table tr:nth-child(2n) {
|
||
background-color: rgb(247, 247, 249);
|
||
}
|
||
.mce-item-table, .mce-item-table td, .mce-item-table th, .mce-item-table caption {
|
||
border: 1px solid #ddd;
|
||
border-collapse: collapse;
|
||
padding: 6px 13px;
|
||
}
|
||
blockquote {
|
||
border-left-width:10px;
|
||
background-color:rgba(128,128,128,0.05);
|
||
border-top-right-radius:5px;
|
||
border-bottom-right-radius:5px;
|
||
padding:15px 20px;
|
||
border-left:5px solid rgba(128,128,128,0.075);
|
||
}
|
||
blockquote p {
|
||
margin-bottom:1.1em;
|
||
font-size:1em;
|
||
line-height:1.45
|
||
}
|
||
blockquote ul:last-child,blockquote ol:last-child {
|
||
margin-bottom:0
|
||
}
|
||
pre {
|
||
padding: 18px;
|
||
background-color: #f7f7f9;
|
||
border: 1px solid #e1e1e8;
|
||
border-radius: 3px;
|
||
display: block;
|
||
}
|
||
code {
|
||
padding: 2px 4px;
|
||
font-size: 90%;
|
||
color: #c7254e;
|
||
white-space: nowrap;
|
||
background-color: #f9f2f4;
|
||
border-radius: 4px;
|
||
}
|
||
.footnote {
|
||
vertical-align: top;
|
||
position: relative;
|
||
top: -0.5em;
|
||
font-size: .8em;
|
||
}
|
||
|
||
hr {
|
||
margin:2em 0
|
||
}
|
||
img {
|
||
max-width:100%
|
||
}
|
||
pre {
|
||
word-break:break-word
|
||
}
|
||
p,pre,pre.prettyprint,blockquote {
|
||
margin:0 0 1.1em
|
||
}
|
||
hr {
|
||
margin:2em 0
|
||
}
|
||
img {
|
||
max-width:100%
|
||
}
|
||
.sequence-diagram,.flow-chart {
|
||
text-align:center;
|
||
margin-bottom:1.1em
|
||
}
|
||
.sequence-diagram text,.flow-chart text {
|
||
font-size:15px !important;
|
||
font-family:"Source Sans Pro",sans-serif !important
|
||
}
|
||
.sequence-diagram [fill="#ffffff"],.flow-chart [fill="#ffffff"] {
|
||
fill:#f6f6f6
|
||
}
|
||
.sequence-diagram [stroke="#000000"],.flow-chart [stroke="#000000"] {
|
||
stroke:#3f3f3f
|
||
}
|
||
.sequence-diagram text[stroke="#000000"],.flow-chart text[stroke="#000000"] {
|
||
stroke:none
|
||
}
|
||
.sequence-diagram [fill="#000"],.flow-chart [fill="#000"],.sequence-diagram [fill="#000000"],.flow-chart [fill="#000000"],.sequence-diagram [fill="black"],.flow-chart [fill="black"] {
|
||
fill:#3f3f3f
|
||
}
|
||
ul,ol {
|
||
margin-bottom:1.1em
|
||
}
|
||
ul ul,ol ul,ul ol,ol ol {
|
||
margin-bottom:1.1em
|
||
}
|
||
kbd {
|
||
padding:.1em .6em;
|
||
border:1px solid rgba(63,63,63,0.25);
|
||
-webkit-box-shadow:0 1px 0 rgba(63,63,63,0.25);
|
||
box-shadow:0 1px 0 rgba(63,63,63,0.25);
|
||
font-size:.7em;
|
||
font-family:sans-serif;
|
||
background-color:#fff;
|
||
color:#333;
|
||
border-radius:3px;
|
||
display:inline-block;
|
||
margin:0 .1em;
|
||
white-space:nowrap
|
||
}
|
||
.toc ul {
|
||
list-style-type:none;
|
||
margin-bottom:15px
|
||
}
|
||
</style>
|
||
<!-- 该css供自定义样式 -->
|
||
<link href="../leanote-html.css" rel="stylesheet">
|
||
</head>
|
||
|
||
<body>
|
||
|
||
<div class="note-container">
|
||
<h1 class="title" id="leanote-title">偷懒-写python脚本时自动添加文件头信息</h1>
|
||
<div class="content-html" id="leanote-content"><pre id="leanote_ace_1503036150695_0" class="brush:python ace-tomorrow">#!/usr/bin/env python
|
||
# -*- coding: UTF-8 -*-
|
||
|
||
# Edit by ZeroC
|
||
# 偷懒-生成python文件头信息
|
||
# AutoCreateHead.py
|
||
|
||
import datetime
|
||
import os
|
||
import platform
|
||
import getpass
|
||
|
||
|
||
# *****查看文件是否存在*****
|
||
# 判断目录是否存在,是否有可访问权限
|
||
def diraccess(dirname):
|
||
# 目录不存在
|
||
disexists = -2
|
||
# 目录无访问权限,有权限为7
|
||
isaccess = -7
|
||
if os.path.exists(dirname):
|
||
if not os.access(dirname, os.W_OK):
|
||
return isaccess
|
||
else:
|
||
isaccess = 7
|
||
return isaccess
|
||
else:
|
||
return disexists
|
||
|
||
|
||
# *****目录处理*****
|
||
# 把路径转化为列表--list();
|
||
# 列表最后添加元素--append();
|
||
# 把列表转换成字符串--''.join()
|
||
def formatdir(dirname = os.path.abspath('.')):
|
||
if platform.system() == 'Linux':
|
||
if dirname.endswith('/'):
|
||
return dirname
|
||
else:
|
||
# 路径后添加/
|
||
listdir = list(dirname)
|
||
listdir.append('/')
|
||
return "".join(listdir)
|
||
elif platform.system() == 'Windows':
|
||
if dirname.endswith('\\'):
|
||
listdir = list(dirname)
|
||
listdir.append('\\')
|
||
return ''.join(listdir)
|
||
else:
|
||
return dirname.rstrip('\\')
|
||
|
||
|
||
# *****文件命名*****
|
||
# 如果flag为1,文件名为filenam当前时间.py;
|
||
# 如果flag为0,文件名为filenam_num.py
|
||
# 当flag为0时,验证文件是否存在,并更换
|
||
def createfile(dirname ,filename = 'ZeroC' ,flag=0 , num = 1 ):
|
||
dname = formatdir(dirname)
|
||
if flag == 1:
|
||
time_now = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
|
||
fname = dname + filename + '_' + time_now + '.py'
|
||
return fname
|
||
elif flag == 0:
|
||
while True:
|
||
fname = dname + filename + '_' + str(num) + '.py'
|
||
daccess = diraccess(dname)
|
||
if daccess == 7:
|
||
if os.path.exists(fname):
|
||
print("The file %s exists!" % fname)
|
||
num += 1
|
||
else:
|
||
break
|
||
elif daccess == -7:
|
||
print("You Deny !")
|
||
chd = raw_input("Change other dir?[y:yes n:no]")
|
||
if chd == 'y':
|
||
Dname = raw_input("The Dirname:")
|
||
dir = formatdir(Dname)
|
||
createfile(dir,fname,0,1)
|
||
else:
|
||
print("You Deny ! Fail to Create file !")
|
||
exit()
|
||
elif daccess == -2:
|
||
os.makedirs(dname)
|
||
createfile(dname,fname,0,1)
|
||
return fname
|
||
|
||
|
||
# *****插入python文件前端公共部分*****
|
||
def inserthead(fn):
|
||
_user = getpass.getuser()
|
||
_host = platform.node()
|
||
_time = datetime.datetime.now().strftime('%Y-%m-%d-%H-%M')
|
||
if platform.system() == 'Linux':
|
||
with open(fn,'a') as f:
|
||
f.write('#!/usr/bin/env python\n# -*- coding: UTF-8 -*-\n\n')
|
||
f.write('#Edit by %s on Linux\n#Hostname:%s\n#Time:%s\n#File:%s' % (_user, _host, _time,fn))
|
||
elif platform.system() == 'Windows':
|
||
with open(fn,'a') as f:
|
||
f.write('#!/usr/bin/env python\n\n# coding:utf-8\n')
|
||
f.write('#Edit by %s on windows\n#Hostname: %s\n#Time:%s\n#File:%s' % (_user, _host, _time, fn))
|
||
|
||
if __name__ == '__main__':
|
||
d = '/root/Python/test/'
|
||
f = 't'
|
||
fN = createfile(d,f,0,1)
|
||
inserthead(fN)</pre><p><br data-mce-bogus="1"></p></div>
|
||
</div>
|
||
|
||
<!-- 该js供其它处理 -->
|
||
<script src="../leanote-html.js"></script>
|
||
</body>
|
||
</html> |