python os path join urlretrieve 文件操作

分析下面的操作:

import os
from urllib3 import request

class file_retrieve():
    def __init__(self):
        self.path = os.path.join(os.path.dirname(os.path.dirname(__file__)),‘images‘)
        print(self.path)
        if not os.path.exists(self.path):
            os.mkdir(self.path)
        print(self.path)

    def process_item(self,item,spider):
        cataory = item[‘category‘]
        urls = item[‘urls‘]

        cataory_path = os.path.join(self.path,cataory)
        if not os.path.exists(cataory_path):
            os.mkdir(cataory_path)
        for url in urls:
            image_name = url.split(‘_‘)[-1]
            request.urlretrieve(url,os.path.join(cataory_path,image_name))

file_retrieve()

os.path.dirname(file) 相当于获取当前的文件的本独立路径,如果要获取到本文件的上一层路径就是 os.path.dirname(os.path.dirname(file))
self.path = os.path.join(路径,文件夹名) join就是为了拼接本地的文件夹,最后就是self.path获取到一个完成的文件夹路径 C:/Users/user/PycharmProjects\images
if not 语句就是为了判断是否为真
os.path.exists(self.path) 是说明这个文件是否存在
os.mkdir(cataory_path) 不存在就创建此文件夹
process_item(self,item,spider): 由于是在类中创建的方法,所以都有self同时在传入冲spider中来的item
image_name = url.split(‘‘)[-1] 把url通过_分割出来变成一个list,同时截取最后一位
request.urlretrieve(url,os.path.join(cataory_path,image_name)) urlretrieve 函数是说明下载url中的地址并保存到对应的文件夹中并命令名字。这样可以保证从url下载过来的文件可以放到正确的文件夹中,并保持文件名正确
urlretrieve(url, filename=None, reporthook=None, data=None) 参数filename:指定了保存本地路径(如果参数未指定,urllib会生成一个临时文件保存数据。)

原文地址:https://blog.51cto.com/zhangfang526/2481005

时间: 2024-07-30 01:03:25

python os path join urlretrieve 文件操作的相关文章

Python——os.path.dirname(__file__) 与 os.path.join(str,str)

Python os.path.dirname(__file__) Python os.path.join(str,str) (1).当"print os.path.dirname(__file__)"所在脚本是以完整路径被运行的, 那么将输出该脚本所在的完整路径,比如: python d:\pythonSrc\test\test.py 那么将输出 d:\pythonSrc\test (2).当"print os.path.dirname(__file__)"所在脚本

Python os.path.dirname(__file__) os.path.join(str,str)

Python os.path.dirname(__file__) Python os.path.join(str,str) (1).当"print os.path.dirname(__file__)"所在脚本是以完整路径被运行的, 那么将输出该脚本所在的完整路径,比如: python d:\pythonSrc\test\test.py 那么将输出 d:\pythonSrc\test (2).当"print os.path.dirname(__file__)"所在脚本

Python os模块--路径、文件、系统命令等操作

os模块包含普遍的操作系统功能. 注意:函数参数path是文件或目录的路径,filename是文件的路径,dirname是目录的路径,路径可以是相对路径,也可绝对路径 常见或重要的函数为加粗字体 os模块: os.name   返回当前操作系统名称('posix', 'nt', 'os2', 'mac', 'ce', 'riscos'),windows为'nt',linux为'posix' os.sep == os.path.sep 返回路径各部分之间的分隔符,linux是'/',windows

Python join() 方法与os.path.join()的区别

Python join() 方法与os.path.join()的区别 1. 函数作用: join() :将序列.字符串 .元组等中的元素以指定的字符连接生成一个新的字符串.os.path.join() : 将多个路径组合后返回 2. join()方法说明: join()方法语法:str.join(sequence)参数说明:str:指定的字符,即分隔符sequence:需要连接的元素 #字符串序列 seq = ("apple", "banana", "pe

python 中os.path.join 双斜杠的解决办法

这两天在写东西的时候遇到了这个问题,主要是上传图片之后,无法在页面展示,原因就出在用join 拼接的路径中出现了"\"而造成的. >>> import os >>> m = os.path.join('路径','文件名.txt') >>> m '路径\\文件名.txt' >>> m.replace('\\','/') '路径/文件名.txt' >>> m = os.path.join('路径','

[python] os.path说明

os.path - Common pathname manipulations操作 This module implements some useful functions on pathnames. To read or write files see open(), and for accessing the filesystem see the os module. The path parameters can be passed as either strings, or bytes.

python os.path模块--转载

os.path.abspath(path) #返回绝对路径 os.path.basename(path) #返回文件名 os.path.commonprefix(list) #返回list(多个路径)中,所有path共有的最长的路径. os.path.dirname(path) #返回文件路径 os.path.exists(path)  #路径存在则返回True,路径损坏返回False os.path.lexists  #路径存在则返回True,路径损坏也返回True os.path.expan

使用 Python 进行稳定可靠的文件操作

点这里 阅读目录 截断-写 写-替换 追加 Spooldir 原子性 一致性 隔离性 程序需要更新文件.虽然大部分程序员知道在执行I/O的时候会发生不可预期的事情,但是我经常看到一些异常幼稚的代码.在本文中,我想要分享一些如何在Python代码中改善I/O可靠性的见解. 考虑下述Python代码片段.对文件中的数据进行某些操作,然后将结果保存回文件中: ? 1 2 3 4 5 with open(filename) as f:    input = f.read() output = do_so

os模块 os.stat('path/filename') os.path.dirname(path) os.path.exists(path)  os.path.join(path1[, path2[, ...]])

提供对操作系统进行调用的接口 1 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 2 os.chdir("dirname")  改变当前脚本工作目录:相当于shell下cd 3 os.curdir  返回当前目录: ('.') 4 os.pardir  获取当前目录的父目录字符串名:('..') 5 os.makedirs('dirname1/dirname2')    可生成多层递归目录 6 os.removedirs('dirname1')    若