1.Python
import os
file_name = os.path.basename(filepath)
file_name_NoExtension = os.path.basename(filepath).split('.')[0]
extension_name = os.path.splitext(filepath)[-1]
2.C语言
#include<stdio.h>
#include<string.h>
char* get_name(char* path, int choice)
{
char pure_file_name[100],file_name[100],extension_name[100];
int i,j,flag = 0;
int len = strlen(path);
for(i=len-1; i>=0; i--)
if(path[i] == '.'){
strcpy(extension_name,&path[i+1]);
break;
}
for(i=len-1; i>=0; i--)
if(path[i] == '\\'){
strcpy(file_name,&path[i+1]);
break;
}
printf("extension_name:%s\n",extension_name);
printf("file_name:%s\n",file_name);
for(i=len-1; i>=0; i--){
if(path[i] == '.' && !flag){
j = i;
flag = 1;
}
if(path[i] == '\\'){
strncpy(pure_file_name,path+i+1,j-i-1);
break;
}
}
printf("pure_file_name:%s\n",pure_file_name);
switch(choice){
case 1:
return file_name;
case 2:
return pure_file_name;
case 3:
return extension_name;
default:
return "choice error!";
}
}
int main(){
char* get_name(char *path, int choice);
char* name = get_name("C:\\Users\\cjk\\desktop\\1.txt", 1);
printf("%s\n",name);
return 0;
}
|