SoFunction
Updated on 2025-04-05

A detailed explanation of the usage of functions in Python

1. Function

Functions are used to splice multiple path strings to generate a new path string.

Usage: (path,*paths)

2. Personal summary of function usage

1. Handle path separators for different operating systems.

2. Look at the parameters from right to left. If a parameter <start> does not have ‘/’ or ‘\’, then use a separator according to the operating system. If it is encountered, all the parameters on the left will be discarded.

3. Look at the parameters from right to left. If there is a parameter that is an absolute path, all parameters on its left will be discarded.

4. If the last parameter is '' empty, the generated path ends with a '\' delimiter (according to the default delimiter of the operating system)

5. There are parameters starting with ‘./’ and ‘/’ at the same time, mainly with ‘/’, starting with ‘/’, starting with the last parameter starting with ‘/’, and all previous parameters are discarded.

6. There is only a subpath starting with ". /" and no subpath starting with " /". The ". " in ". /" will be spliced ​​as part of the subpath.

3. Detailed explanation of the usage example of functions

This computer operating system: Win10

1. Handle path separators for different operating systems.

 Functions can automatically select appropriate path separators based on the operating system. For example, Windows uses backslash\’,Unix and Mac use forward slashes/

This computer operating system: Win10, so it can only display the basic usage

Demo1

import os

print(('path', 'www', 'xxx', 'iii'))

# Output:path\www\xxx\iii

2. Look at the parameters from right to left. If a parameter <start> does not have a forward slash ‘/’ or a backslash ‘\’, then use a separator according to the operating system. If it is encountered, all the parameters on the left will be discarded.

Demo2

import os

print(('path', 'www', 'xxx', 'iii'))  
# Output: path\www\xxx\iii
print(('path', 'www', 'xxx', '/iii'))  
# Output: /iii
print(('path', 'www', '/xxx', 'iii'))  
# Output: /xxx\iii
print(('path', '/www', 'xxx', 'iii'))
# Output: /www\xxx\iii
print(('path', '\www', 'xxx', 'iii'))
# Output: \www\xxx\iii
print(('p/a/t/h/', 'www/', 'x/x/x/', 'ii/i'))  # If all use ‘/’, add / at the end of the parameter, otherwise it will become \ according to the operating system.# Output:p/a/t/h/www/x/x/x/ii/i

3. Look at the parameters from right to left. If there is a parameter that is an absolute path, all parameters on its left will be discarded.

Demo3

import os

print(('path', 'www', 'xxx', 'iii'))
# Output: path\www\xxx\iii
print(('path', 'w:ww', 'xxx', 'iii'))
# Output: w:ww\xxx\iii
print(('path', 'w:\ww', 'x:xx', 'iii'))
# Output: x:xx\iii
print(('path', 'w:\ww', 'x:xx', '/iii'))
# Output: x:/iii
print(('path', 'w:\ww', 'xxx', '/iii'))
# Output: w:/iii
print(('path', 'w:\ww', 'xxx', ''))
# Output: w:\ww\xxx\

4. If the last parameter is '' empty, the generated path ends with a '\' separator

 Demo4

import os

print(('path', 'www', 'xxx', 'iii'))
# Output: path\www\xxx\iii
print(('path', 'www', 'xxx', 'iii', ''))
# Output:path\www\xxx\iii\

5. At the same time, there are parameters starting with ‘./’ and ‘/’, mainly with ‘/’, and splicing starts from the last parameter starting with ‘/’, and all previous parameters are discarded.

Demo5

import os

print(('path', 'www', 'xxx', 'iii'))
# Output: path\www\xxx\iii
print(('path', './www', '/xxx', 'iii'))
# Output: /xxx\iii
print(('path', './www', '/xxx/', 'iii'))
# Output: /xxx/iii
print(('path', 'www', '/xxx', './iii'))
# Output: /xxx\./iii
print(('path', 'www', '/xxx/', './iii'))
# Output:/xxx/./iii

6. There is only a subpath starting with ". /" and no subpath starting with " /". The " . " in ". /" will be spliced ​​as part of the subpath.

Demo6

import os

print(('path', 'www', 'xxx', 'iii'))
# Output: path\www\xxx\iii
print(('path', 'www', 'xxx', './iii'))
# Output: path\www\xxx\./iii
print(('path', 'www', './xxx', 'iii'))
# Output:path\www\./xxx\iii

Summarize

This is all about this article about the usage of functions in Python. For more related content on Python function usage, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!