pitón en os.path.split uso de la función ()

concepto básico

El os.path.split () dividido por el nombre de ruta de la de un par de la cabeza y la cola de la lista. Si la cola de la lista de elementos últimos de nombre de ruta. elemento de cabeza está en frente de ella.

Por ejemplo:

path name = '/home/User/Desktop/file.txt'

En el ejemplo anterior, el nombre de ruta se llama la cola ruta archivo.txt '/ home / usuario / Escritorio / ' se llama la cabeza. pieza de la cola no contendrá una barra oblicua. Si este nombre de ruta termina en una barra, a continuación, la cola está vacía.
Si no hay una barra en el camino, entonces la cabeza está vacía. Estos son los parámetros que se detallan:

    path                             head                 tail
'/home/user/Desktop/file.txt'   '/home/user/Desktop/'   'file.txt'
'/home/user/Desktop/'           '/home/user/Desktop/'    {empty}
'file.txt'                           {empty}            'file.txt'

Estudio de caso

1 Ejemplo uno:

# Python program to explain os.path.split() method      
# importing os module  
import os 
  
# path 
path = '/home/User/Desktop/file.txt'
  
# Split the path in  
# head and tail pair 
head_tail = os.path.split(path) 
  
# print head and tail 
# of the specified path 
print("Head of '% s:'" % path, head_tail[0]) 
print("Tail of '% s:'" % path, head_tail[1], "\n") 
  
'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:579817333 
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''  
# path 
path = '/home/User/Desktop/'
  
# Split the path in  
# head and tail pair 
head_tail = os.path.split(path) 
  
# print head and tail 
# of the specified path 
print("Head of '% s:'" % path, head_tail[0]) 
print("Tail of '% s:'" % path, head_tail[1], "\n") 
  
# path 
path = 'file.txt'
  
# Split the path in  
# head and tail pair 
head_tail = os.path.split(path) 
  
# print head and tail 
# of the specified path 
print("Head of '% s:'" % path, head_tail[0]) 
print("Tail of '% s:'" % path, head_tail[1]) 

2 resultados:

Head of '/home/User/Desktop/file.txt': /home/User/Desktop
Tail of '/home/User/Desktop/file.txt': file.txt 

Head of '/home/User/Desktop/': /home/User/Desktop
Tail of '/home/User/Desktop/':  

Head of 'file.txt': 
Tail of 'file.txt': file.txt

Ejemplos de dos 3

# Python program to explain os.path.split() method  
'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:579817333 
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''      
# importing os module  
import os 
  
# path 
path = '' 
  
# Split the path in  
# head and tail pair 
head_tail = os.path.split(path) 
  
# print head and tail 
# of the specified path 
print("Head of '% s':" % path, head_tail[0]) 
print("Tail of '% s':" % path, head_tail[1]) 
  
  
# os.path.split() function 
# will return empty 
# head and tail if  
# specified path is empty 

4 Resultados del ensayo:

Head of '': 
Tail of '':
Publicados 706 artículos originales · ganado elogios 829 · Vistas 1,32 millones +

Supongo que te gusta

Origin blog.csdn.net/sinat_38682860/article/details/105125313
Recomendado
Clasificación