获取目录下所有文件名
c++
#include <io.h>
#include <fstream>
#include <string>
#include <vector>
#include <iostream>
using namespace std;
// 得到路径path下的指定格式的 所有文件文件名
void getJustCurrentFiles(string path, vector<string> & files)
{
//文件句柄
long hFile = 0;
//文件信息
struct _finddata_t fileinfo;
string p;
if((hFile = _findfirst(p.assign(path).append("\\*.txt").c_str(),&fileinfo)) != -1)
{
do
{
if((fileinfo.attrib & _A_SUBDIR)){ }
else
{
files.push_back(fileinfo.name);
// 完整路劲
// files.push_back(p.assign(path).append("\\").append(fileinfo.name) );
}
}while(_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
}
}
int main(int argc, char const *argv[])
{
std::vector<string> fs;
getJustCurrentFiles("E:\\",fs);
for (std::vector<string>::iterator i = fs.begin(); i != fs.end(); ++i)
{
cout<<*i<<endl;
}
return 0;
}
python
import os
def getfilelist(path,file_form):
f_list = os.listdir(path)
for l in f_list[:]:
if os.path.splitext(l)[1] == file_form :
print l
#print f_list
if __name__ == '__main__':
getfilelist('E:\\','.txt')