Python IO

 
## a simple example rename a file 
import os
import shutil from os 
import path

def main():
# make a duplicate of an existing file 
if path.exists("guru99.txt"):
# get the path to the file in the current directory 
src = path.realpath("guru99.txt");
# rename the original file 
os.rename("career.guru99.txt","guru99.txt")
if __name__ == "__main__": main()

another example of OS modual:

## listdir func
files = os.listdir(os.curdir)
for file in files:
    if '.pnt' in file:
        newfile = file.replace('.pnt', '.txt')
        os.rename(file, newfile)
## glob func
import os,glob
os.chdir(dir)
for fi in glob.glob("*.pnt"):
   os.rename(fi, fi[:-3] + "txt") 
os.chdir(value)
for aFile in glob.glob("*.php"):
    base = os.path.splitext(aFile)[0]
os.rename(aFile, base + ".null")
## join two string as a pretty path 
import os,sys
   folder = 'E:/.../1936342-G/test'
   for filename in os.listdir(folder):
           infilename = os.path.join(folder,filename)
           if not os.path.isfile(infilename): continue
           oldbase = os.path.splitext(filename)
           infile= open(infilename, 'r')
           newname = infilename.replace('.grf', '.las')
           output = os.rename(infilename, newname)
           outfile = open(output,'w')
## A For loop get attrS
## splitext func
import os, sys

for filename in os.listdir(os.path.dirname(os.path.abspath(__file__))):
  base_file, ext = os.path.splitext(filename)
  if ext == ".txt":
    os.rename(filename, base_file + ".text")
  
## a fixed example

for filename in glob.iglob(os.path.join(folder, '*.grf')):

    os.rename(filename, filename[:-4] + '.las')
 
  




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值