my python script (1) ---------create model script file

# my  python script

#  to create script file

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import os,subprocess

child = subprocess.Popen(‘which python‘,stdout = subprocess.PIPE,shell=True)
(pypath,error) = child.communicate()
pypath = pypath.strip(‘\n‘)

myfile = raw_input(‘please input file name (like home/test.py)‘)
(path,filename) = os.path.split(myfile)
if path:
    print ‘check dir path ...‘
    if os.path.exists(path):
        print ‘dir already exists!‘
    else:
        print ‘dir: %s does not exist!‘ % path
        print ‘create dir: %s ...‘ % path
        try:
            os.makedirs(path)
            print ‘dir: %s is created successfully!‘ % path
        except Exception ,e:
            print ‘exception occur while try to create dirs %s‘ % path
            print e
else:
    pass

print ‘create script file: %s ...‘ % filename
try:
    f = open(myfile,‘w‘)
    f.write(‘#!‘+ pypath + ‘\n‘)
    f.write(‘#! -*- coding:utf-8 -*-‘)
    f.write(‘\n‘)
    f.write(‘\n‘)
    f.write(‘if __name__ == ‘+ "‘__main__‘:\n")
    f.write(‘    print "hello world!"\n‘)
    f.close()
    print ‘script is created successfully!‘
except Exception,e:
    print ‘exception occur while try to write file: %s ‘ % filename
    print e

# victor

时间: 2024-12-23 14:11:24

my python script (1) ---------create model script file的相关文章

How do I create a .pyc file?

Python automatically compiles your script to compiled code, so called byte code, before running it. When a module is imported for the first time, or when the source is more recent than the current compiled file, a .pyc file containing the compiled co

File upload error - unable to create a temporary file

php上传图片的时候会报错: File upload error - unable to create a temporary file 文件上传错误 - 无法创建一个临时文件 你只需要打开你的php.ini文件,设置upload_tmp_dir="f:\temp"  然后重启环境就可以了,如果还不行就去看看文件夹的权限是否有权限,赋予写入权限就好了

window下编写python脚本在linux下运行出错 usr/bin/python^M: bad interpreter: No such file or directory

今天在windows下使用notepad++写了个python脚本,传到linux服务器执行后提示:-bash: ./logger.py: usr/bin/python^M: bad interpreter: No such file or directory 1.原因分析 这是不同系统编码格式引起的:在windows系统中编辑的logger.py文件可能有不可见字符,所以在Linux系统下执行会报以上异常信息.一般是因为windows行结尾和linux行结尾标识不同造成的,在window中行结

mysql报Can't create/write to file '/tmp/ib0n3frL' (Errcode: 13 - Permission denied)

初始化数据库报错 [[email protected] mysql-5.6.24]# /usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data Installing MySQL system tables...2017-04-03 19:52:21 0 [Warning] TIMESTAMP with implicit DEFA

哪种写法更好?<script></script> vs/or <script type=”text/javasript”></script>

一直很奇怪 哪种写法更好<script type="text/javascript">-</script> or <script>-</script>? 大致解释就是 如果用H5 就不需要加,因为H5默认属性就是text/javascript, 而 HTML 4.01 and XHTML 1.0 规范 type 属性是需要的,如果用H5<script>...</script> 是可以且比较好的选择. 参考:http

ERROR 1 (HY000): Can&#39;t create/write to file &#39;/tmp/#sql_830_0.MYI&#39; (Errcode: 13)

mysql操作时,出现报错. 执行describe 命令时, 临时文件目录没有创建或者无写入权限:于是: cd /var/lib/mysql/ #进入mysql数据目录 mkdir tmp #创建需要的临时目录 chown mysql.mysql /var/lib/mysql/tmp #给临时目录分配mysql权限用户及群组 ERROR 1 (HY000): Can't create/write to file '/tmp/#sql_830_0.MYI' (Errcode: 13)

python出现Non-ASCII character &#39;\xe7&#39; in file ex6.py on line 1, but no encoding declare错误

http://www.cnblogs.com/qi09/archive/2012/02/06/2340712.html python中出现Non-ASCII character '\xe7' in file ex6.py on line 1, but no encoding declare的错误 可按照错误建议网址查看http://www.python.org/peps/pep-0263.html 发现是因为Python在默认状态下不支持源文件中的编码所致.解决方案有如下三种: 一.在文件头部添

编译安装mysql ERROR: 1 Can&#39;t create/write to file &#39;/tmp/#sql_86b_0.MYI&#39;

之前安装一个5.1版本,试了一个5.5版本,useradd -s /sbin/nologin -M mysql   创建一个mysql用户mkdir -p /data/mysql chmod -R /data/mysql cd /usr/local/mysqlscripts/mysql_install_db --user=mysql --datadir=/data/mysql出现报错:ERROR: 1  Can't create/write to file '/tmp/#sql_86b_0.MY

ERROR 1 (HY000): Can&#39;t create/write to file &#39;/tmp/#sql_909_0.MYI&#39; (Errcode: 13)

mysql> desc tablename; ERROR 1 (HY000): Can't create/write to file '/tmp/#sql_909_0.MYI' (Errcode: 13) 两种解决办法:方法一:1.chmod 777 /tmp[[email protected] /]# chmod 777 /tmpchmod: 更改"/tmp" 的权限: 只读文件系统 --遇到这种情况,tmp权限无法更改. 2./etc/init.d/mysqld start