python文件小号单排。。。。(这局没打好。。。-25)

#GetFuncInPy.py  获取函数

import sys
import re
import os

print()
if len(sys.argv) != 2:
    print ("Error Usage!please use as :GetFuncInPy.py os.py")
    exit()
path = "C:\\Python33\\Lib\\"<span style="white-space:pre">	</span>#Your python's installation file name
if not os.path.isfile(path + sys.argv[1]):
    print ("'%s' is not a file!"%sys.argv[1])
    exit()
fp = open(path + sys.argv[1] )

#output functions whose name is not "_xxxx"
for line in fp.readlines():
    if line[0]!= ' ' and "def " in line and not"def _" in line:
        print(line.strip())
close(fp)



#p = re.compile(r"def \w+\(.*\):")   #我原本想用高端的RE做的。。。结果好难写正则表达式
#p.matchall(sys.argv[1])

然后python GetFuncInpy.py os.py来看看os.py里面都有什么函数,结果如下:


然后我就知道了:我搞错了!这个os.py是进程控制用的!不是文件打开关闭读写那个!!!原来文件的打开关闭是内置的!!!我不知道去哪里找这些不需要import的东西的源码!我就找了网上的一些东西。。。

······文件打开:open(name , type)                             #type 可为 "r","w","r+",“w+”,"rb+","wb+"等,“+”表示读写都有,"b"表示二进制打开。至于二进制和字符串形式文件的区别,我查一下:然后我浅显的理解了一下:一个("r")是以2byte为单位读的,一个 ("rb")是以byte为单位读的。而且读出的东西不一样,前者是str,后者是bytes。


······文件读写:read([size])     write(str)

还有一些额外的  readline()   readlines()   ,readlines()常见用法就是 

for line in f.readlines():
    print(line.strip())  #或者其他对line的操作,strip可以去掉最后的‘\n’


·······文件指针相关:tell()      seek(offset [,whence = 1])  # offset 表示指针转移到whence后offset偏移量处,whence = 0 :从文件起始处开始, = 1 从当前处开始, =2 从文件尾处开始(文件尾部向后搜索有什么用?)





-25.。。。。


发布了25 篇原创文章 · 获赞 4 · 访问量 8万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章