How does shutil.move (src,dst) identify paths?

1. On the python file operation function shutil.move [src.dst] question, src must specify the path.
2. I want to know why f..python in shutil.move can identify the path of f? I don"t think there is any logic here. Because the f here does not assemble any paths. It"s just an f variable in for loop..
3. The file in the following code was copied successfully, but on the surface, f did not get any path, how to determine the f path? no, no, no.
Code hint:
`
import shutil
import os

path =". /"- there are several files or folders under the current path of sharp.
files = os.listdir (path)

for f in files:

folder_name = os.path.join("./",f.split(".")[-1])
if not os.path.exists(folder_name):
    os.makedirs(folder_name)
    shutil.move(f,folder_name)
else:
    shutil.move(f,folder_name)
    
   `
Mar.23,2021

this can be understood as a file path resolution problem and applies to all file operations, not just shutil.move () .

File paths can be divided into two categories:

  1. absolute path
    that is, files can be found directly according to this path, regardless of the current directory of the process.
    such as "C:\ Windows\ system32\ user32.dll"
  2. relative path
    the actual file location is synthesized by the process's current directory . For example:

    < table > < thead > < tr > < th > current directory < / th > < th > relative path < / th > < th > actual file location < / th > < / tr > < / thead > < tbody > < tr > < td > C:\ Windows\ < / td > < td > user32.dll < / td > < td > C:\ Windows\ user32.dll < / td > < / tr > < tr > < td > C:\ Windows\ system32\ < / td > < td > user32.dll < / td > < td > C:\ Windows\ system32\ user32.dll < / td > < / tr > < tr > < td > C:\ Windows\ system32\ < / td > < td >..\ user32.dll < / td > < td > C:\ Windows\ user32.dll < / td > < / tr > < / tbody > < / table >

    in python, use os.getcwd () to get the current directory of the process, and os.path.join () to synthesize the path.


shutil this standard library is implemented in python code. If you want to know its principle, you can take a look at the source code: python/cpython/blob/master/Lib/shutil.py-sharpL659" rel=" nofollow noreferrer "> python/cpython/blob/master/Lib/shutil.py-sharpL659" rel= "nofollow noreferrer" > https://github.com/python/cpy.

.
Menu