python 文件操作实例
1. 重命名文件 (Windows 平台)
import os path = "C:\\_Writing\\referee" os.chdir(path) folders = os.listdir(path) for folder in folders: newName = folder[-4:]+' '+folder[:-4] os.rename(folder,newName)
2. 复制文件到相应文件夹,并运行(Windows 平台)
import os import shutil import win32api subName = 'Dec041' cpfiles0 = ['input_filename','Pvol.dat', '0parameters.in'] inputFiles = ['0.2299', '0.2834', '0.3711', '0.4394', '0.5357', '0.6002', '0.6400', '0.4749', '0.6650', '0.6850', '0.7050'] path0 = "C:\\0pore\\poreFcodes\\Pore-drag\\11drag\\Debug" file0 = os.path.join(path0, '11drag.exe') for i in range(7): # for i in range(6,11): inputFile = inputFiles[i]+'.dat' cpfiles = cpfiles0 cpfiles[0] = inputFile pathOld = "C:\\0pore\\0drag\\" + inputFiles[i] pathNew = pathOld + '\\' + subName os.mkdir(pathNew) for f in cpfiles: fileOld = os.path.join(pathOld, f) fileNew = os.path.join(pathNew, f) shutil.copyfile(fileOld, fileNew) fileNew = os.path.join(pathNew, '11drag.exe') shutil.copyfile(file0, fileNew) os.chdir(pathNew) pathExe = os.path.join(pathNew, '11drag.exe') win32api.ShellExecute(0, 'open', pathExe, '', '', 1)
3. 删除目录下指定文件外的所有文件 (Linux 平台)
#!/usr/bin/python import os Keepfiles = ['Bo00100', 'hopp3d.inp'] delDir = "/short/v71/ywu565/settling/quenching/0JUN/NP6500/Bo00100" delList = [] delList = os.listdir(delDir) for f in delList: if f in Keepfiles: continue delFile = os.path.join(delDir, f) if os.path.isfile(delFile): os.remove(delFile)
4. 读取指定文件内的内容,并提取数据做出图形
# -*- coding: utf-8 -*- """ Created on Mon Jan 29 10:46:12 2018 @author: yongliw """ import numpy as np import pandas as pd import matplotlib.pyplot as plt path0 = 'F:\\0settling\\0AUG10\\0coarse' classname = ['D1000f','S125f','S1000f','S1500f','S2000f','S2400f'] Folders = ['00','03','05','07','10']; content = '0TimeRho.txt' figLayout = '32' plt.figure(1, figsize=(12, 18)) l0 = len(classname) l = len(Folders) for j in range(l0): print '================ ', classname[j][0:-1], ' ================' packing_density = np.ones(l) friction = np.array([0, 0.3, 0.5, 0.7, 1.0]) for i in range(l): pathi = path0+'\\'+classname[j] + Folders[i] + '\\' + content f = open(pathi) t = f.readline() packing_density[i] = float(t[17:24]) print 'Friction = ',friction print classname[j][0:-1], ' = ', packing_density data = np.hstack((friction.reshape(l,1),packing_density.reshape(l,1))) Result = pd.DataFrame(data,columns=['Friction','Packing_density']) print Result sname = figLayout + str(j+1) plt.subplot(sname) plt.title(classname[j][0:-1]) plt.scatter(friction,packing_density) plt.ylabel('Packing density') plt.xlabel('Friction')
以上运行结果见如下,.html文件:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd"> <html><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="qrichtext" content="1" /><style type="text/css"> p, li { white-space: pre-wrap; } </style></head><body style=" font-family:'Consolas'; font-size:10pt; font-weight:400; font-style:normal;"> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">Python 2.7.13 |Anaconda 4.4.0 (32-bit)| (default, May 11 2017, 14:07:41) [MSC v.1500 32 bit (Intel)]</p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">Type "copyright", "credits" or "license" for more information.</p> <p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br /></p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">IPython 5.3.0 -- An enhanced Interactive Python.</p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">? -> Introduction and overview of IPython's features.</p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">%quickref -> Quick reference.</p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">help -> Python's own help system.</p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">object? -> Details about 'object', use 'object??' for extra details.</p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br />Restarting kernel... </p> <hr /> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br /> </p> <p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br /></p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" color:#000080;">In [</span><span style=" font-weight:600; color:#000080;">1</span><span style=" color:#000080;">]:</span> runfile('F:/0settling/0AUG10/0coarse/readRho.py', wdir='F:/0settling/0AUG10/0coarse')</p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">================ D1000 ================</p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">Friction = [ 0. 0.3 0.5 0.7 1. ]</p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">D1000 = [ 0.63868 0.57132 0.55796 0.55714 0.55141]</p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"> Friction Packing_density</p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">0 0.0 0.63868</p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">1 0.3 0.57132</p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">2 0.5 0.55796</p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">3 0.7 0.55714</p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">4 1.0 0.55141</p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">================ S125 ================</p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">Friction = [ 0. 0.3 0.5 0.7 1. ]</p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">S125 = [ 0.63793 0.61659 0.60977 0.60541 0.60813]</p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"> Friction Packing_density</p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">0 0.0 0.63793</p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">1 0.3 0.61659</p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">2 0.5 0.60977</p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">3 0.7 0.60541</p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">4 1.0 0.60813</p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">================ S1000 ================</p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">Friction = [ 0. 0.3 0.5 0.7 1. ]</p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">S1000 = [ 0.63649 0.57104 0.56614 0.56327 0.56245]</p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"> Friction Packing_density</p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">0 0.0 0.63649</p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">1 0.3 0.57104</p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">2 0.5 0.56614</p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">3 0.7 0.56327</p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">4 1.0 0.56245</p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">================ S1500 ================</p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">Friction = [ 0. 0.3 0.5 0.7 1. ]</p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">S1500 = [ 0.63384 0.57377 0.57159 0.56552 0.5643 ]</p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"> Friction Packing_density</p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">0 0.0 0.63384</p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">1 0.3 0.57377</p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">2 0.5 0.57159</p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">3 0.7 0.56552</p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">4 1.0 0.56430</p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">================ S2000 ================</p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">Friction = [ 0. 0.3 0.5 0.7 1. ]</p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">S2000 = [ 0.63813 0.56961 0.57125 0.57411 0.56818]</p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"> Friction Packing_density</p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">0 0.0 0.63813</p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">1 0.3 0.56961</p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">2 0.5 0.57125</p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">3 0.7 0.57411</p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">4 1.0 0.56818</p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">================ S2400 ================</p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">Friction = [ 0. 0.3 0.5 0.7 1. ]</p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">S2400 = [ 0.63895 0.6155 0.60813 0.60841 0.6065 ]</p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"> Friction Packing_density</p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">0 0.0 0.63895</p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">1 0.3 0.61550</p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">2 0.5 0.60813</p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">3 0.7 0.60841</p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">4 1.0 0.60650</p> <p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br /></p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><img src="packing_density_files/qt_img205600084459524.png"></p> <p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br /></p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" color:#000080;">In [</span><span style=" font-weight:600; color:#000080;">2</span><span style=" color:#000080;">]:</span> </p></body></html>

浙公网安备 33010602011771号