python dirname_os.walk(dirpath,dirname,filename)用法

一、先介绍下file、seek、truncate的基本用法:

seek(offset,where):  where=0从起始位置移动,1从当前位置移动,2从结束位置移动。当有换行时,会被换行截断。seek()无返回值,故值为None。

tell():  文件的当前位置,即tell是获得文件指针位置,受seek、readline、read、readlines影响,不受truncate影响

truncate(n):  从文件的首行首字符开始截断,截断文件为n个字符;无n表示从当前位置起截断;截断之后n后面的所有字符被删除。其中win下的换行代表2个字符大小。

readline(n):读入若干行,n表示读入的最长字节数。其中读取的开始位置为tell()+1。当n为空时,默认只读当前行的内容

readlines读入所有行内容

read读入所有行内容

二、以下以1个例子说明以上各个函数的作用

fso = open("f:\\a.txt",'w+')    '以w+方式,并非a方式打开文件,故文件原内容被清空

print fso.tell()    '文件原内容被清空,故此时tell()=0

fso.write("abcde\n")  '写入文件abcde\n,因为换行\n占两个字符,故共写入7个字符

print fso.tell()  '此时tell()=7

fso.write("fghwm")  '又写入文件fghwm,故此时文件共写入7+5 =142个字符

print fso.tell()  '此时tell()=12

fso.seek(1, 0)  '从起始位置即文件首行首字符开始移动1个字符

print fso.tell()   ‘此时tell() =1

print  fso.readline()  '读取当前行,即文件的第1行,但是从第二个字符(tell()+1)开始读,结果为:bcde。

'若换成for读取整个文件或read读取整个文件则结为bcdefghwm

print fso.tell()   ‘因为readline此时tell() =7,

fso.truncate(8)  '从写入后文件的首行首字符开始阶段,截断为8个字符,即abcde\nf,即文件的内容为:abcde\nf

print fso.tell()   ‘tell() 依旧为7,并为受truncate(8)影响,但是此时文件内容为abcde\nf

print  fso.readline()  ‘从tell()+1=8开始读取,读取当前行内容:f

fso.close()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
解释一下“def main(): newBricks = packageVersionsFromConfig() newDict = getDependencies(newBricks, "davinci") sipver = newDict.get(SIP_PACKAGE_NAME) sipver =sipver['release'] logging.info("BRICKS SIP package version is %s", sipver) handleRepo(SIP_PACKAGE_NAME, False) logging.info("Cloning SIP repo, hold your horses, this could take a while (1.8G)") repo = git.Repo(os.path.join(GIT_WORKING_DIR, SIP_PACKAGE_NAME)) repo.git.checkout(sipver) logging.info ("Git status of repo %s is %s ", SIP_PACKAGE_NAME, repo.git.status()) #copying files to SIP folder sipVersion = [] reg_compile = re.compile("SIP_*") for dirpath, dirnames, filenames in os.walk(os.path.join(GIT_WORKING_DIR, SIP_PACKAGE_NAME, "src")): sipVersion = sipVersion + [dirname for dirname in dirnames if reg_compile.match(dirname)] logging.info("Vector SIP version is %s", sipVersion) if input("WARNING !!!! You're about to overwrite your local SIP folder. Are you sure you want to continue ? (y/n)") != "y": exit("I wish you good fortune") for dirpath, dirnames, filenames in os.walk(os.path.join("src","SIP")): for filename in filenames: if filename == "CMakeLists.txt": continue absfile = os.path.join(dirpath, filename) #logging.info(absfile) os.remove(absfile) for dirname in dirnames: if dirname == "Components": continue absdir = os.path.join(dirpath, dirname) #logging.info(absfile) shutil.rmtree(absdir) logging.info("Copying source files from Vector SIP cloned repo to the SIP folder") distutils.dir_util.copy_tree(os.path.join(GIT_WORKING_DIR, SIP_PACKAGE_NAME, "src", sipVersion[0]), os.path.join("src","SIP")) ”
最新发布
07-11

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值