Configuration file reading points

Python supports reading configuration files at the end of.ini and.conf, and keeping the configuration file external facilitates subsequent modifications. Relative path is usually used when reading configuration files. In this case, the absolute path of the current file folder should be obtained by using the following code:

path = os.path.dirname(__file__)

If you want to get the relative path of the current file, change dirname to abspath. Here is the complete code to read the configuration file:

Import configparser import OS import shutil # to create the configuration file read object cf = configparser. The configparser # () to obtain the absolute path to the current folder path = Os.path.dirname (__file__) print(path) # Cf. Read (path + "/config.ini")

Note that ConfigParser calls the read() method to read the file before performing any subsequent operations on it. The configuration file format is as follows:

[filePath]
sourcePath = E:/testCopyFile/sourceDir
destPath = E:/testCopyFile/destDir/

The important thing to note in configuration files is that strings do not need to be quoted.

Copy part

Python has the shutil package to support file operations. Copy the folder using the method copyTree (). Note that the copy will fail when the target folder exists. In this case, you need to modify the source code to avoid this problem (shutil.py source) :

def _copytree(entries, src, dst, symlinks, ignore, copy_function, ignore_dangling_symlinks, dirs_exist_ok=False): if ignore is not None: ignored_names = ignore(os.fspath(src), [x.name for x in entries]) else: Ignored_names = set() # If not os.path.exists(DST); ignored_names = set() # If not os.path.exists(DST); os.makedirs(dst, exist_ok=dirs_exist_ok) errors = [] use_srcentry = copy_function is copy2 or copy_function is copy for srcentry in entries: if srcentry.name in ignored_names: continue srcname = os.path.join(src, srcentry.name) dstname = os.path.join(dst, srcentry.name) srcobj = srcentry if use_srcentry else srcname try: is_symlink = srcentry.is_symlink() if is_symlink and os.name == 'nt': # Special check for directory junctions, which appear as # symlinks but we want to recurse. lstat = srcentry.stat(follow_symlinks=False) if lstat.st_reparse_tag == stat.IO_REPARSE_TAG_MOUNT_POINT: is_symlink = False if is_symlink: linkto = os.readlink(srcname) if symlinks: # We can't just leave it to `copy_function` because legacy # code with a custom `copy_function` may rely on copytree # doing the right thing. os.symlink(linkto, dstname) copystat(srcobj, dstname, follow_symlinks=not symlinks) else: # ignore dangling symlink if the flag is on if not os.path.exists(linkto) and ignore_dangling_symlinks: continue # otherwise let the copy occur. copy2 will raise an error if srcentry.is_dir(): copytree(srcobj, dstname, symlinks, ignore, copy_function, dirs_exist_ok=dirs_exist_ok) else: copy_function(srcobj, dstname) elif srcentry.is_dir(): copytree(srcobj, dstname, symlinks, ignore, copy_function, dirs_exist_ok=dirs_exist_ok) else: # Will raise a SpecialFileError for unsupported file types copy_function(srcobj, dstname) # catch the Error from the recursive copytree so that we can # continue with other files except Error as err: errors.extend(err.args[0]) except OSError as why: errors.append((srcname, dstname, str(why))) try: copystat(src, dst) except OSError as why: # Copying file access times may fail on Windows if getattr(why, 'winerror', None) is None: errors.append((src, dst, str(why))) if errors: raise Error(errors) return dst

The other thing is that the source folder is not created in the destination folder, which means that this method is equivalent to copying everything in the source folder into the destination folder. Since I need the source folder in the destination folder, I need to create it separately:

sourcePath = cf.get('filePath', 'sourcePath')
destRootPath = cf.get('filePath', 'destPath')
destCopyPath = destRootPath + '/sourcePath'

if not os.path.exists(destCopyPath):
    os.makedirs(destCopyPath)
shutil.copytree(sourcePath, destCopyPath)