引言最近在学习Python,每次在新建Python文件时都会在文件的开头部分加入如下的信息,然后将文件添加可执行权限。自然,这个用程序自动生成最好不过了。所以,一开始就使用了Shell脚本实现一个版本,基本工作正常。今天就想着用Python实现一个对应的版本吧,于是乎就开始做了;虽然很简单的逻辑,很简单的代码,不过还是让我这个新手吃了一点亏。虽然比较曲折,但是还是可以工作了。以下把遇到的问题即解决办法记录下来。
#!/usr/bin/env python3
# -*-coding: utf-8-*-
# author: Chris
# blog: http://www.cnblogs.com/chriscabin/
# create_time: 2015-09-05 10:01:42
'''
Add description for this module.
'''
__author__ = "Christopher Lee"
问题一 Python运行linuxshell 命令主要有以下几种(将在学习后期做一个系统性的总结):
os.system()
os.popen()
subPRocess.call()问题二 为何使用vim打开“创建”后的文件却并没有内容写入呢最初版本的实现代码示例如下:def create_py_file():
file_name='demo.py'
# 判断参数个数
args = sys.argv
if len(args) == 2:
file_name = args[1] + '.py'
if len(args) <= 2:
try:
with open(file_name, mode='w') as out_file:
# 向文件中写入信息
custom_print = partial(print, file=out_file)
custom_print('#!/usr/bin/env python3')
custom_print('# -*-coding: utf-8-*-')
# 此处省略。。。
# 打开文件
command = 'vim %s +11' % (file_name)
os.system(command)
except Exception as e:
print(e.message)
else:
print("Too many arguments.")
if __name__ == '__main__':
create_py_file()
现象说明本以为在custom_print执行后,文件中已经写入相关信息了,但是在执行完vim file_name命令后发现并没有内容。非常纳闷寻找原因仔细分析后,才发现实际上在执行打开文件命令时,之前的信息并没有写入到新的文件中。因为在with语句块结束前,文件流并没有被关闭,也就没有保存到磁盘中,所以才导致问题的发生。(注意上面的代码的18、19行,明显还在with块中)解决办法其实解决办法超级简单,只需要将18、19行代码往前缩进即可!最终版本的实现代码最终版本的代码可以完成预期的要求:可以创建一个新的文件(如果文件存在,则直接打开),向其中添加头部的信息;最后给文件添加可执行权限,并且使用vim打开。
只需要把文件移动到/usr/local/bin/,并且命名为new即可。这样,只需要在终端中运行new demo就可以轻松创建一个新的python文件啦!#!/usr/bin/env python3
# -*-coding: utf-8-*-
# author: Chris
# blog: http://www.cnblogs.com/chriscabin/
# create_time: 2015-09-05 10:25:46
'''
This is a python script, which aims to create a new python file automaticly.
'''
__author__ = "Christopher Lee"
import sys
import time
from functools import partial
import os
def create_py_file():
file_name = 'demo.py'
# 判断参数个数
args = sys.argv
if len(args) == 2:
file_name = args[1]
if len(args) <= 2:
try:
if os.path.exists(file_name):
print("File exists.")
command = 'vi %s' % file_name
os.system(command)
return None
with open(file_name, mode='w') as out_file:
# 向文件中写入信息
custom_print = partial(print, file=out_file)
custom_print('#!/usr/bin/env python3')
custom_print('# -*-coding: utf-8-*-')
custom_print('# author: Chris')
custom_print('# blog: http://www.cnblogs.com/chriscabin/')
custom_print('# create_time:', time.strftime('%Y-%m-%d %H:%M:%S'))
custom_print('')
custom_print("'''\nAdd description for this module.\n'''")
custom_print('')
custom_print('__author__ = "Christopher Lee"')
custom_print('')
# 当文件流关闭后再进行操作
# 添加可执行权限
command = 'chmod a+x %s' % file_name
os.system(command)
command = 'vim %s +12' % (file_name)
os.system(command)
except Exception as e:
print(e.message)
else:
print("Too many arguments.")
if __name__ == '__main__':
create_py_file()