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

本文介绍了一种跨平台的方法来获取当前程序所在目录及其包含的所有文件。通过使用C++在Windows和Linux环境下实现,并提供了Python和Shell脚本的等效实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


注:头文件请自行查询

说明

  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  os
def 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
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值