c python shell获得文件路径以及所有的文件
来源:互联网 发布:免费源码分享网站 编辑:程序博客网 时间:2024/06/06 02:22
- 说明
- windows平台
- linux平台
- python
- shell
注:头文件请自行查询
说明
- 功能:获取程序所在的目录以及该目录下所有文件
windows平台
void getFullPath() { wchar_t wcPath[BUF_LEN] = { 0 }; wchar_t wcDire[BUF_LEN] = { 0 }; char cPath[BUF_LEN] = { 0 }; char cDire[BUF_LEN] = { 0 }; GetCurrentDirectory(BUF_LEN, wcDire); GetModuleFileName(NULL, wcPath, BUF_LEN); printf("fileDire = %ls\n", wcDire); printf("filePath = %ls\n", wcPath); vector<_finddata_t> vFiles; sprintf_s(cDire, "%ws", wcDire); //wchar to char// wsprintf(wcDire, L"%hs", cDire); //char to wchar getFiles(cDire, vFiles); for (unsigned int i = 0; i < vFiles.size(); ++i) { printf("filename = %s; filesize = %d\n", vFiles[i].name, vFiles[i].size); }}void getFiles(const char *fileDire, vector<_finddata_t> &vFiles) { struct _finddata_t fileData; char pDire[BUF_LEN] = { 0 }; memcpy(pDire, fileDire, strlen(fileDire)); strcat_s(pDire, "\\*"); int hFile = _findfirst(pDire, &fileData); if (hFile == -1) { printf("findfirst(%s, \&fileData) failedn", pDire); return ; } else { do { if (fileData.attrib & _A_SUBDIR || strcmp(fileData.name, ".") == 0 || strcmp(fileData.name, "..") == 0) { //printf("fileDire %s\n", fileData.name); } else { //printf("file %s\n", fileData.name); vFiles.push_back(fileData); } } while (_findnext(hFile, &fileData) == 0); } _findclose(hFile);}
linux平台
void showDire(){ char pathCur[BUF_LEN]; memset(pathCur, 0, BUF_LEN); getcwd(pathCur, BUF_LEN); printf("\npath: %s\n", pathCur); showFile(pathCur);}void showFile(char *path){ DIR *pDir; struct dirent *pdirent; if ((pDir = opendir(path)) == NULL){ printf("open dir error, %s\n", strerror(errno)); return ; } printf("\nfiles in %s:\n", path); while ((pdirent = readdir(pDir)) != NULL){ if (pdirent->d_name[0] == '.') continue; printf("%s ", pdirent->d_name); } printf("\n");}
python
import osdef showPath(): fileDir = os.getcwd() print (fileDir) filesUnderDir = os.listdir(fileDir) print (filesUnderDir) for file in filesUnderDir: if os.path.isdir(os.path.join(fileDir, file)): print ("this is dir : %s" % file) else: print ("this is file: %s" % file)
shell
function showPath(){ pwd pwd | sed 's/^\(.*\)[/]//'# sed 's/oldstr/newstr/g' 替换# sed 's/oldstr//' 删除# ^word 字符串从行首开始找# \(.*\) \表示转义,(.*)表示全部字符# \(.*\)[/] 表示在字符 / 之前的所有字符 for file in ls; do if test -f $file; then echo $file is a file elif test -d $file; then echo $file is a dir fi done}
阅读全文
0 0
- c python shell获得文件路径以及所有的文件
- python获得路径和子路径下所有的文件的相关信息
- SHELL 脚本获取当前所有文件以及路径
- Python读取路径下的所有文件
- 获得某路径下的特定格式的所有文件
- C++获得文件夹下所有文件的路径及文件名
- C#--如何获得文件保存路径的.txt文件
- 如何使用Python获取某个路径下面所有文件的绝对路径以及其他操作
- linux shell脚本获得当前文件路径
- shell脚本获得文件完整路径
- shell递归遍历某路径下所有的文件
- python中OS模块获得文件路径的流程
- 获得class文件的路径
- 获得资源配置文件的路径
- windows下获得一个指定文件路径下的所有文件
- objective-c如何获得文件路径
- Python获取文件夹下所有的文件路径列表
- python获取指定路径下的所有文件
- dfs学习之n皇后问题
- lcp计数 (Trie)
- python爬虫实战二——股票数据定向爬虫【有补充】
- 笔记:SEO在网站制作中的应用
- 第十一课、C判断
- c python shell获得文件路径以及所有的文件
- 文章标题
- c++静态函数和变量
- 各数据类型大小
- pata1056
- 应用统计学与R语言实现学习笔记(十三)——因子分析
- 排序算法(不稳定)
- linux 内核启动 arm64 汇编 head.s
- 详解C中volatile关键字