Showing posts with label File exist. Show all posts
Showing posts with label File exist. Show all posts

9/20/2020

python change file name, get file name, dir, ext, check file exist in source code using os package

 

get file name and ext

import os
os.path.splitext("/path/to/some/file.txt")[0]
#/path/to/some/file
base = os.path.basename('/root/dir/sub/file.ext')
#'file.ext'
os.path.splitext(base)
#('file', '.ext')
os.path.splitext(base)[0]
#'file'

get dir

os.path.dirname("/path/to/some/file.txt")
#'/path/to/some'

change file name 

os.rename(r'C:\Users\Ron\Desktop\Test\Products.txt',r'C:\Users\Ron\Desktop\Test\Shipped Products.txt')


check file exist

os.path.isfile('./path_of_file')




1/03/2017

In MFC, File exist check and delete the file, example source code.



If file exist then delete the file, example source code in MFC

< github code >
___

12/28/2016

File exist check code



bool fileExists(const char* path)
{
  FILE* file;
  bool exists;

  file = fileOpen(path, "r");
  exists = file != 0;

  if (file)
    fclose(file);

  return exists;
}

FILE* fileOpen(const char* path, const char* mode)
{
  char path_[PATH_MAX];

  assert(path);

  resolvePath(path_, sizeof(path_), path);

  FILE* fp;
  fopen_s(&fp, path_, mode);
  return fp;
}