導航:首頁 > 文件目錄 > 用循環讀取文件路徑

用循環讀取文件路徑

發布時間:2023-07-29 04:58:20

Ⅰ Python使用for循環依次打開該目錄下的各文件

importos
path=r"F:Python第一周作業 ask"
otherpath=r"F:Python其它目錄"
forfilenameinos.listdir(path):
print(path,filename)
fullname=os.path.join(path,filename)
ifos.path.isfile(fullname):
othername=os.path.join(otherpath,filename)
otherfile=open(othername,'wb')
forlineinopen(fullname,'rb'):
forcinline:
ifnotc.isdigit():otherfile.write(c)
otherfile.close()

java 循環讀取文件夾裡面的文件

JAVA 遍歷文件夾下的所有文件(遞歸調用和非遞歸調用)
1.不使用遞歸的方法調用。
public void traverseFolder1(String path) {
fileNum = 0, folderNum = 0;
File file = new File(path);
if (file.exists()) {
LinkedList<File> list = new LinkedList<File>();
File[] files = file.listFiles();
for (File file2 : files) {
if (file2.isDirectory()) {
System.out.println("文件夾:" + file2.getAbsolutePath());
list.add(file2);
fileNum++;
} else {
System.out.println("文件:" + file2.getAbsolutePath());
folderNum++;
}
}
File temp_file;
while (!list.isEmpty()) {
temp_file = list.removeFirst();
files = temp_file.listFiles();
for (File file2 : files) {
if (file2.isDirectory()) {
System.out.println("文件夾:" + file2.getAbsolutePath());
list.add(file2);
fileNum++;
} else {
System.out.println("文件:" + file2.getAbsolutePath());
folderNum++;
}
}
}
} else {
System.out.println("文件不存在!");
}
System.out.println("文件夾共有:" + folderNum + ",文件共有:" + fileNum);

}
2.使用遞歸的方法調用
public static List<File> getFileList(String strPath) {
File dir = new File(strPath);
File[] files = dir.listFiles(); // 該文件目錄下文件全部放入數組
if (files != null) {
for (int i = 0; i < files.length; i++) {
String fileName = files[i].getName();
if (files[i].isDirectory()) { // 判斷是文件還是文件夾
getFileList(files[i].getAbsolutePath()); // 獲取文件絕對路徑
} else if (fileName.endsWith("avi")) { // 判斷文件名是否以.avi結尾
String strFileName = files[i].getAbsolutePath();
System.out.println("---" + strFileName);
filelist.add(files[i]);
} else {
continue;
}
}

}
return filelist;
}

Ⅲ 如何用C語言循環讀取文件內容

  1. 循環讀取一個文件的內容。

    這樣做沒有任何意義,不如把讀取的內容保持在變數當中,節省空間時間。如果非要反復讀取的話可以使用rewind函數把文件指針重置。

    函數名: rewind()
    功 能: 將文件內部的位置指針重新指向一個流(數據流/文件)的開頭
    注意:不是文件指針而是文件內部的位置指針,隨著對文件的讀寫文件的位置指針(指向當前讀寫位元組)向後移動。而文件指針是指向整個文件,如果不重新賦值文件指針不會改變。rewind函數作用等同於 (void)fseek(stream, 0L, SEEK_SET);

  2. 循環讀取多個文件的內容。

    把多個文件名存在一個字元串數組當中。使用循環語句反復打開-讀取-關閉即可。

    例如:

    char*s[3]={"文件一","文件二","文件三"};
    for(i=0;i<3;i++){
    f=fopen(s[i]);//打開第i個文件
    //讀取數據
    fclose(f);//關閉文件
    }

Ⅳ C語言中怎麼實現循環讀取文件.文件名為A_in1.txt,A_in2.txt,A_in3.txt,A_in4.txt

#include<stdio.h>
#include<string.h>
voidmain()
{
inti=1;
charstr[20];
for(;i<=4;i++)
{
memset(str,0,20);
sprintf(str,"A_in%d.txt",i);
printf("%s ",str);
}
getchar();
}

是不是這個效果,照著例子解決文件路徑的字元串剩下的就好辦了!

還有不懂的再追問

閱讀全文

與用循環讀取文件路徑相關的資料

熱點內容
蘋果5s16g升級ios103 瀏覽:850
word紅頭文件中怎麼加雙線 瀏覽:825
切割機用什麼編程 瀏覽:787
文件修訂題目 瀏覽:572
魅族pro5自帶瀏覽器怎麼升級 瀏覽:342
為什麼用數據還是載入慢 瀏覽:171
監控下面有網路如何隔離 瀏覽:748
前台中jrequestjson 瀏覽:435
怎麼在word里粘貼xls文件 瀏覽:710
買火車票什麼網站 瀏覽:757
jsp的六大 瀏覽:216
圖文數據要如何導入資料庫 瀏覽:207
軟體數據刪了恢復怎麼弄 瀏覽:517
小米筆記本電腦共享文件 瀏覽:812
cad字體文件夾添加國標字體 瀏覽:938
電腦輸出pdf文件有什麼用 瀏覽:695
微信滿人 瀏覽:585
js點擊按鈕下載圖片 瀏覽:922
數據有效性哪裡設置的 瀏覽:171
orl刪除表的資料庫 瀏覽:353

友情鏈接