国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

[全網唯一]通過修改源碼使得從ZIP提取文件并在提取時進行重命名保存(博客園同步發(fā)布)

這篇具有很好參考價值的文章主要介紹了[全網唯一]通過修改源碼使得從ZIP提取文件并在提取時進行重命名保存(博客園同步發(fā)布)。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

源碼位置: /Lib/zipfile.py/ZipFile/_extract_member/zipfile.py或者直接點擊extract函數(shù).

在使用python解壓縮zip文件時, 由于需要在解壓時重命名文件為我想要的格式, 而不巧的是, zipfile包官方源代碼沒有這個功能...

于是, 在百度之后, 果斷放棄尋找現(xiàn)成代碼的想法.

在研究了一下extract函數(shù)的原源代碼后, 覺得可以加一個參數(shù)targetname用來指代重命名后的文件名, 而很巧的是, 這個新參數(shù)并沒有在源代碼中使用, 所以加入它沒有影響.

Talk is easy, show you code~

代碼展示

row 1618
    def extract(self, member, path=None, pwd=None,targetname=None):
        """targetname : the name extracted rename to targetname
        
            Extract a member from the archive to the current working directory,
           using its full name. Its file information is extracted as accurately
           as possible. `member' may be a filename or a ZipInfo object. You can
           specify a different directory using `path'.
        """
        if path is None:
            path = os.getcwd()
        else:
            path = os.fspath(path)
 
        return self._extract_member(member, path, pwd,targetname)
 
    def extractall(self, path=None, members=None, pwd=None,targetname=None):
        """Extract all members from the archive to the current working
           directory. `path' specifies a different directory to extract to.
           `members' is optional and must be a subset of the list returned
           by namelist().
        """
        if members is None:
            members = self.namelist()
 
        if path is None:
            path = os.getcwd()
        else:
            path = os.fspath(path)
 
        for zipinfo in members:
            self._extract_member(zipinfo, path, pwd,targetname)
row 1650
...
row 1665
def _extract_member(self, member, targetpath, pwd,targetname):
        """Extract the ZipInfo object 'member' to a physical
           file on the path targetpath.
        """
        if not isinstance(member, ZipInfo):
            member = self.getinfo(member)
 
        # build the destination pathname, replacing
        # forward slashes to platform specific separators.
        arcname = member.filename.replace('/', os.path.sep)
 
        if os.path.altsep:
            arcname = arcname.replace(os.path.altsep, os.path.sep)
        # interpret absolute pathname as relative, remove drive letter or
        # UNC path, redundant separators, "." and ".." components.
        arcname = os.path.splitdrive(arcname)[1]
        invalid_path_parts = ('', os.path.curdir, os.path.pardir)
        arcname = os.path.sep.join(x for x in arcname.split(os.path.sep)
                                   if x not in invalid_path_parts)
        if os.path.sep == '\\':
            # filter illegal characters on Windows
            arcname = self._sanitize_windows_name(arcname, os.path.sep)
        if targetname is None:
            targetpath = os.path.join(targetpath, arcname)
            targetpath = os.path.normpath(targetpath)
        else:
            targetpath = os.path.normpath(targetpath)
 
        # Create all upper directories if necessary.
        upperdirs = os.path.dirname(targetpath)
        if upperdirs and not os.path.exists(upperdirs):
            os.makedirs(upperdirs)
 
        if member.is_dir():
            if not os.path.isdir(targetpath):
                os.mkdir(targetpath)
            return targetpath
        if targetname is None:
            with self.open(member, pwd=pwd) as source, \
                open(targetpath, "wb") as target:
                shutil.copyfileobj(source, target)
        else:
            with self.open(member, pwd=pwd) as source, \
                open(targetpath+"/"+targetname, "wb") as target:
                shutil.copyfileobj(source, target)
 
        return targetpath
row 1713

  用法

可以直接粘貼到自己的源碼中, 如果擔心出現(xiàn)其他bug, 可以用完就重裝zipfile.

調用extract時傳入三個參數(shù): 壓縮包所在目錄, 目標目錄, 目標名稱(重命名后的名字, 如果為None則默認原命名)文章來源地址http://www.zghlxwxcb.cn/news/detail-711176.html

到了這里,關于[全網唯一]通過修改源碼使得從ZIP提取文件并在提取時進行重命名保存(博客園同步發(fā)布)的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!

本文來自互聯(lián)網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。如若轉載,請注明出處: 如若內容造成侵權/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經查實,立即刪除!

領支付寶紅包贊助服務器費用

相關文章

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領取紅包,優(yōu)惠每天領

二維碼1

領取紅包

二維碼2

領紅包