python学习笔记
#还是2个参数的都用命令行来传吧 # -*- coding: utf-8 -*- import os,sys CDROM ='e:\windows' def cdWalker(cdrom,cdcfile): export="" for root,dirs,files in os.walk(cdrom): export+="\n %s;%s;%s" % (root,dirs,files) open(cdcfile,'w').write(export) #cdWalker('e:\windows','cd1.cdc') cdWalker(sys.argv[1],sys.argv[2])
python学习笔记之傻瓜错误
代码来自可爱的python
# -*- coding: utf-8 -*- import os,sys CDROM ='e:\windows' def cdWalker(cdrom,cdcfile): export="" for root,dirs,files in os.walk(cdrom): export+="\n %s;%s;%s" % (root,dirs,files) open(cdcfile,'w').write(export) #cdWalker('e:\windows','cd1.cdc') if "-e"==sys.argv[1]: cdWalker(CDROM,sys.argv[2]) print "记录光盘信息到 %s" %sys.argv[2] else: print '''PyCDC使用方式: python pycdc.py -e mycd1-1.cdc #将光盘内容记录为mycd1-1.cdc '''
然后先前我设置了环境变量,结果就直接cmd下执行python e:\pycdc1.py -e e:\mycd1.cdc
后来一次不小心关掉cmd之后,我想起了Python(command line) 打开之后我输入了同样的命令
>>> python e:\pycdc1.py -e e:\mycd1.cdc File "<stdin>", line 1 python helloworld.py ^ SyntaxError: invalid syntax
谷歌之
the >>> prompt indicates you are already running python interpreter.
You need to enter that command from the shell prompt.
Press Ctrl-D to exit Python to the ordinary shell prompt and try again.
Python 幽默剧?
Python学习手册上的一个笑话 看不懂
在Python学习手册的第33页
送给Python代码的例子加入一种幽默的特质。比如,一般说来,传统的变量名为"foo"和"bar",在Python的世界中变成了"spam"和"eggs"。而有时出现的"Brian"、"ni"、"shrubbery"等也是这样来的,这种方法设置很大程度上影响了Python社区:Python会议上的演讲往往叫做"The Spanish Inquisition" 。
当然,如果你熟悉这个幽默剧的话,所有这些你都会觉得很有趣,否则就那么有意思了。你没有必要为了理解引自Monty Python(也许这本书中你就会找到)的例子而刻意去熟悉这一串剧情,但是至少你现在应该知道它们的来源。
有点好奇,虽然以后可能会在书中看到,我还是打算先把这个记录下来。。
第一个python程序
很早很早就想学python,一直没开动,今天突然兴起,照着书敲了2个demo,第一个if语句那一直报错,不知道错哪。。。。
#!/usr/bin/env python 'makeTextFile.py -- create text file' import os ls = os.linesep #get filename while True: if os.path.exists(fname): print "ERROR: '%s' already exists" %fname else: break #get file content (text) lines all = [] print "\nEnter lines ('.' by itself to quit).\n" #loop until user terminates input while True: entry = raw_input('>') break; else: all.append(entry) #write lines to file with proper line ending fobj = open(fname,'w') fobj.writelines(['%s%s' % (x,ls) for x in all]) fobj.close() print 'DONE!'
第2个 可以运行了。。
#!/user/bin/env python 'readTextFile.py -- read and display text file' # get filename fname = raw_input('Enter filename:') print #attempt to open file for reading try: fobj = open(fname,'r') except IOError,e: print "***file open error:",e else: #display contents to the screen for eachLine in fobj: print eachLine, fobj.close()
先这样了 还要做俯卧撑,然后洗澡睡觉~
2011-08-23 09:13
经过网友的热心帮助 修改makeTestFile.py如下
#!/usr/bin/env python 'makeTextFile.py -- create text file' import os ls = os.linesep #get filename while True: if os.path.exists(fname): print "ERROR: '%s' already exists" %fname else: break #get file content (text) lines all = [] print "\nEnter lines ('.' by itself to quit).\n" #loop until user terminates input while True: entry = raw_input('>') break; else: all.append(entry) #write lines to file with proper line ending fobj = open(fname,'w') fobj.writelines(['%s%s' % (x,ls) for x in all]) fobj.close() print 'DONE!'