Today the internship company assigned a data Processing task. When I connect a string in a list to a long path, I encounter the following problem:
Import ospath_list = [' first_directory ', ' second_directory ', ' file.txt ']print os.path.join (path_list)
After discovering Os.path.join, it is still a list of strings. That's what I was wondering:
[' First_directory ', ' second_directory ', ' file.txt ']
After thinking about it, Os.path.join's input must be one or more str, not a list. The nature of the list of strings is still lists. instruction to interpret a string list as a str, which is equivalent to os.path.join a single str, and, of course, no change at all.
So I changed the code:
Import ospath_list = [' first_directory ', ' second_directory ', ' file.txt ']# print os.path.join (path_list) head = ' for path ' In path_list: head = os.path.join (head, path) print head
Finally, the string in the list is concatenated into a full long path:
First_directory/second_directory/file.txt