Python path splicing os.path.join()

os.path.join() function: connect two or more pathname components

1. If the first letter of each component name does not contain'/', the function will automatically add
2. If a component is an absolute path, all components before it will be discarded
3. If the last component is empty , The generated path ends with a'/' separator

用法:
ebert_review_urls = [
‘https://d17h27t6h515a5.cloudfront.net/topher/2017/September/59ad9900_1-the-wizard-of-oz-1939-film/1-the-wizard-of-oz-1939-film.txt’, ‘https://d17h27t6h515a5.cloudfront.net/topher/2017/September/59ad9901_2-citizen-kane/2-citizen-kane.txt’, ‘https://d17h27t6h515a5.cloudfront.net/topher/2017/September/59ad9901_3-the-third-man/3-the-third-man.txt’]

folder_name = ‘ebert_reviews’
if not os.path.exists(folder_name):
os.makedirs(folder_name)

for url in ebert_review_urls:
response = requests.get(url)
with open(os.path.join(folder_name,url.split(’/’)[-1]),mode = ‘wb’) as file:
file.write(response.content)

Guess you like

Origin blog.csdn.net/weixin_45281949/article/details/99689428