## 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
| |