I'm connected to Azure Storage and needs to get the name of a file. I have written a function which gets the list of contents in a particular directory.
datepath = np.datetime64('today').astype(str).replace('-', '/')
def list_directory_contents():
try:
file_system_client = service_client.get_file_system_client(file_system="container_name")
paths = file_system_client.get_paths(path = "folder_name/" + str(datepath))
for path in paths:
print(path.name + '\n')
except Exception as e:
print(e)
And then I'm calling the function
list_directory_contents()
This gives me the something like
folder_name/2020/10/28/file_2020_10_28.csv
Now I want to extract just the file name from the above ie "file_2020_10_28.csv"
You're looking foros.path.basename
. It's a cross platform and robust way to do what you want-
>>> os.path.basename("folder_name/2020/10/28/file_2020_10_28.csv")
'file_2020_10_28.csv'
in case it wasn't obvious, the part to be changed in list_directory_contents
to print only the basename is this-
for path in paths:
print(os.path.basename(path.name) + '\n')
assuming path.name
is the string that returns something akin to folder_name/2020/10/28/file_2020_10_28.csv
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.