c python shell获得文件路径以及所有的文件

来源:互联网 发布:免费源码分享网站 编辑:程序博客网 时间:2024/06/06 02:22

  • 说明
  • windows平台
  • linux平台
  • python
  • shell

注:头文件请自行查询

说明

  1. 功能:获取程序所在的目录以及该目录下所有文件

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}