檔案操作函式加例項

2021-08-18 19:38:16 字數 985 閱讀 3706

import os

os.rename(srcname,destname)

import os

os.remove(srcname)

##3.建立資料夾

import os

os.mkdir(destfolder)

import os

os.rmdir(destfolder)

import os

os.getcwd()

import os

os.chdir("../")

import os

os.listdir("./")

import os

import shutil

#判斷當前路徑是否存在新三國的資料夾

dir_list = os.listdir("./")

for filelist in dir_list:

if filelist == "新三國":

#刪除非空資料夾

shutil.rmtree("./新三國")

break

os.mkdir("新三國")

os.chdir("新三國")

#在新三國裡建立五個文字

list = ["1", "2", "3", "4", "5"]

for i in list:

f = open("新三國-"+i+".txt", "w")

f.close()

dirlist = os.listdir("./")

for name in dirlist:

newname = "[東哥出品]-" + name

os.rename(name, newname)

檔案操作例項

開發資料查詢 define crt secure no warnings include include include include include define path kaifang.txt char g pp 全域性的二級指標 int imax 20151574 標示有多少行 int g...

Java檔案操作例項

統計指定資料夾大小 public static long getfilelength file dir else return len 刪除指定資料夾 public static void deletefile file dir else 5,迴圈結束後,把空資料夾刪掉 dir.delete 把乙個...

PHP常用檔案操作函式和簡單例項分析

php最常用的檔案操作就是讀取和寫入了,今天就主要講解一下讀取和寫入函式,並且做乙個頁面訪問的計數功能,來記錄乙個頁面的訪問量。fopen php中程式設計客棧沒有檔案建立函式,建立和開啟檔案都用fopen 函式,函式的形式為 resource fopen string filename,strin...