当前位置: 首页 > news >正文

建设电影会员网站软文推广代写代发

建设电影会员网站,软文推广代写代发,网站关键词标题怎么写,网站安全狗 服务名接上节课内容,在开始正式移动文件到目标文件夹之前,我们需要再思考一个问题。在代码运行之前,阿文的下载文件夹里已经存在一些分类文件夹了,比如图例中“PDF文件”这个文件夹就是已经存在的。这样的话,在程序运行时&am…

接上节课内容,在开始正式移动文件到目标文件夹之前,我们需要再思考一个问题。
在代码运行之前,阿文的下载文件夹里已经存在一些分类文件夹了,比如图例中“PDF文件”这个文件夹就是已经存在的。
这样的话,在程序运行时,按目前的逻辑,计算机就会把这个文件夹分类为其他文件,因为这个文件夹没有后缀名。

因此,每运行一遍代码,之前已经创建的文件夹都会被移动到“其他文件”这个文件夹下。
为了避免将文件夹移动到其他文件中,我们需要先判断该文件是否是文件夹,不是文件夹再移动。

修改代码

使用os.path.join()函数拼接文件路径,并赋值给变量itemPath。

我们先使用 os.path.join() 函数拼接出文件目前所在路径,也就是使用下载文件夹的路径文件名进行合并。图例中我们先将合并后的文件路径赋值给变量 itemPath ,然后使用print输出itemPath。

# 使用import导入os模块

import os

# 将阿文的下载文件夹路径 /Users/yequ/Downloads 赋值给变量downloadPath

downloadPath = "/Users/yequ/Downloads"

# 使用os.listdir()函数获取该路径下所有的文件(夹),并赋值给变量allItems

allItems = os.listdir(downloadPath)

# 使用for循环遍历所有文件(夹)

for item in allItems:

    # 获取文件后缀名

    extension = os.path.splitext(item)[1].lower()

    # 定义一个变量targetPath,用来表示准备移动到的文件夹路径

    targetPath = ""

    if extension in [".jpg", ".jpeg", ".gif", ".png", ".bmp"]:

        # 使用os.path.join()函数拼接分类文件夹路径:图片文件

        targetPath = os.path.join(downloadPath, "图片文件")

    elif extension in [".avi", ".mp4", ".wmv", ".mov", ".flv"]:

        # 使用os.path.join()函数拼接分类文件夹路径:视频文件

        targetPath = os.path.join(downloadPath, "视频文件")

    elif extension in [".wav", ".mp3", ".mid", ".ape", ".flac"]:

        # 使用os.path.join()函数拼接分类文件夹路径:音频文件

        targetPath = os.path.join(downloadPath, "音频文件")

    elif extension in [".pdf"]:

        # 使用os.path.join()函数拼接分类文件夹路径:PDF文件

        targetPath = os.path.join(downloadPath, "PDF文件")

    elif extension in [".docx", ".doc"]:

        # 使用os.path.join()函数拼接分类文件夹路径:Word文件

        targetPath = os.path.join(downloadPath, "Word文件")

    elif extension in [".xlsx", ".xls"]:

        # 使用os.path.join()函数拼接分类文件夹路径:Excel文件

        targetPath = os.path.join(downloadPath, "Excel文件")

    elif extension in [".pptx", ".ppt"]:

        # 使用os.path.join()函数拼接分类文件夹路径:PPT文件

        targetPath = os.path.join(downloadPath, "PPT文件")

    else:

        # 使用os.path.join()函数拼接分类文件夹路径:其他文件

        targetPath = os.path.join(downloadPath, "其他文件")

    # 如果目标文件夹不存在,使用os.mkdir()函数创建文件夹

    if not os.path.exists(targetPath):

        os.mkdir(targetPath)

    # 使用os.path.join函数拼接文件路径,并赋值给变量itemPath

    itemPath = os.path.join(downloadPath, item)

   

    # 使用print()输出文件路径

    print(itemPath)

判断路径是否是文件夹

我们将要进行判断的文件路径传入到 os.path.isdir() 函数中,如果该路径是文件夹就会返回True,如果不是就返回False。

判断当itemPath路径是一个文件夹时,输出变量itemPath。

将文件路径传入 os.path.isdir() 函数中,由于该函数返回的是布尔数
我们可以使用 if 语句进行判断,当结果是 True 时,也就是该路径是一个文件夹时,使用print输出itemPath。

# 使用import导入os模块

import os

# 将阿文的下载文件夹路径 /Users/yequ/Downloads 赋值给变量downloadPath

downloadPath = "/Users/yequ/Downloads"

# 使用os.listdir()函数获取该路径下所有的文件(夹),并赋值给变量allItems

allItems = os.listdir(downloadPath)

# 使用for循环遍历所有文件(夹)

for item in allItems:

    # 获取文件后缀名

    extension = os.path.splitext(item)[1].lower()

    # 定义一个变量targetPath,用来表示准备移动到的文件夹路径

    targetPath = ""

    if extension in [".jpg", ".jpeg", ".gif", ".png", ".bmp"]:

        # 使用os.path.join()函数拼接分类文件夹路径:图片文件

        targetPath = os.path.join(downloadPath, "图片文件")

    elif extension in [".avi", ".mp4", ".wmv", ".mov", ".flv"]:

        # 使用os.path.join()函数拼接分类文件夹路径:视频文件

        targetPath = os.path.join(downloadPath, "视频文件")

    elif extension in [".wav", ".mp3", ".mid", ".ape", ".flac"]:

        # 使用os.path.join()函数拼接分类文件夹路径:音频文件

        targetPath = os.path.join(downloadPath, "音频文件")

    elif extension in [".pdf"]:

        # 使用os.path.join()函数拼接分类文件夹路径:PDF文件

        targetPath = os.path.join(downloadPath, "PDF文件")

    elif extension in [".docx", ".doc"]:

        # 使用os.path.join()函数拼接分类文件夹路径:Word文件

        targetPath = os.path.join(downloadPath, "Word文件")

    elif extension in [".xlsx", ".xls"]:

        # 使用os.path.join()函数拼接分类文件夹路径:Excel文件

        targetPath = os.path.join(downloadPath, "Excel文件")

    elif extension in [".pptx", ".ppt"]:

        # 使用os.path.join()函数拼接分类文件夹路径:PPT文件

        targetPath = os.path.join(downloadPath, "PPT文件")

    else:

        # 使用os.path.join()函数拼接分类文件夹路径:其他文件

        targetPath = os.path.join(downloadPath, "其他文件")

    # 如果目标文件夹不存在,使用os.mkdir()函数创建文件夹

    if not os.path.exists(targetPath):

        os.mkdir(targetPath)

    # 使用os.path.join函数拼接文件路径,并赋值给变量itemPath

    itemPath = os.path.join(downloadPath, item)

    # 使用if判断itemPath路径是一个文件夹时

    if os.path.isdir(itemPath):

        # 使用print()输出itemPath

        print(itemPath)

移动文件夹

要想移动文件,我们需要导入Python中另一个内置的模块 shutil ,然后使用 shutil.move() 函数来对文件进行移动。
shutil.move() 函数可以用来移动文件或文件夹。它接收两个参数,第一个参数是要移动的文件(夹)路径,第二个参数是目标文件(夹)的路径。

代码修改

导入shutil模块。
判断当itemPath不是文件夹时,使用shutil.move()函数移动itemPath路径的文件到targetPath路径的文件夹。

阿文的需求,是把分好类的文件移动到对应的文件夹里。

所以我们只需在程序判断 itemPath 不是一个文件夹的时候,使用 shutil.move() 函数将itemPath路径的文件移动到targetPath路径的文件夹下。

# 使用import导入os模块

import os

# 使用import导入shutil模块

import shutil

# 将阿文的下载文件夹路径 /Users/yequ/Downloads 赋值给变量downloadPath

downloadPath = "/Users/yequ/Downloads"

# 使用os.listdir()函数获取该路径下所有的文件(夹),并赋值给变量allItems

allItems = os.listdir(downloadPath)

# 使用for循环遍历所有文件(夹)

for item in allItems:

    # 获取文件后缀名

    extension = os.path.splitext(item)[1].lower()

    # 定义一个变量targetPath,用来表示准备移动到的文件夹路径

    targetPath = ""

    if extension in [".jpg", ".jpeg", ".gif", ".png", ".bmp"]:

        # 使用os.path.join()函数拼接分类文件夹路径:图片文件

        targetPath = os.path.join(downloadPath, "图片文件")

    elif extension in [".avi", ".mp4", ".wmv", ".mov", ".flv"]:

        # 使用os.path.join()函数拼接分类文件夹路径:视频文件

        targetPath = os.path.join(downloadPath, "视频文件")

    elif extension in [".wav", ".mp3", ".mid", ".ape", ".flac"]:

        # 使用os.path.join()函数拼接分类文件夹路径:音频文件

        targetPath = os.path.join(downloadPath, "音频文件")

    elif extension in [".pdf"]:

        # 使用os.path.join()函数拼接分类文件夹路径:PDF文件

        targetPath = os.path.join(downloadPath, "PDF文件")

    elif extension in [".docx", ".doc"]:

        # 使用os.path.join()函数拼接分类文件夹路径:Word文件

        targetPath = os.path.join(downloadPath, "Word文件")

    elif extension in [".xlsx", ".xls"]:

        # 使用os.path.join()函数拼接分类文件夹路径:Excel文件

        targetPath = os.path.join(downloadPath, "Excel文件")

    elif extension in [".pptx", ".ppt"]:

        # 使用os.path.join()函数拼接分类文件夹路径:PPT文件

        targetPath = os.path.join(downloadPath, "PPT文件")

    else:

        # 使用os.path.join()函数拼接分类文件夹路径:其他文件

        targetPath = os.path.join(downloadPath, "其他文件")

    # 如果目标文件夹不存在,使用os.mkdir()函数创建文件夹

    if not os.path.exists(targetPath):

        os.mkdir(targetPath)

    # 使用os.path.join()函数拼接文件路径,并赋值给变量itemPath

    itemPath = os.path.join(downloadPath, item)

    # 判断当itemPath路径不是文件夹时,移动文件到分类文件夹去

    if not os.path.isdir(itemPath):

        # 使用shutil.move()函数移动文件到targetPath路径

        shutil.move(itemPath, targetPath)

shutil.move()函数可以用来移动文件或文件夹。它接收两个参数,第一个参数是要移动的文件(夹)路径,第二个参数是目标文件(夹)的路径。

至此,我们帮助阿文编写的,对文件自动分类并移动的代码就全部完成啦。可以发现,几乎是一瞬间,几十个文件就全部分类并移动到指定的文件夹了。

http://www.hengruixuexiao.com/news/9238.html

相关文章:

  • 日本人做网站上海网站快速排名优化
  • 河北手机网站制作哪家好网站建设黄页在线免费
  • 沈阳工伤保险做实在哪个网站百度服务中心电话
  • dedecms5.7 整个网站 css和js代码怎么优化网络营销在哪里学比较靠谱
  • jsp做网站的书seo点击工具帮你火21星热情
  • 天津个人网站建设网站排名前十
  • 如何让企业网站如何写推广软文
  • 贵阳网站建设推广微信视频号怎么推广引流
  • 文广网站建设百度官网推广
  • 咸宁网站建设网站案例分析
  • 做3D打印样品用什么外贸网站好百度浏览器极速版
  • 深圳 网站开发公司湖南seo优化
  • email注册网站外贸谷歌seo
  • 石家庄网页定制开发宜昌seo
  • 网站打开慢原因高级搜索引擎
  • 创建网站大约多少钱网上接单平台有哪些
  • 手机响应式网站开发永久免费客服系统有哪些软件
  • 山东网站建设哪家权威流量宝官网
  • 官方网站开发用什么语言广州网络推广外包
  • 网站开发宣传公司网站设计与制作
  • 商会网站建设最新注册域名查询
  • 网站一键收录网站结构优化
  • 网上花店 网站源代码广州优化防控措施
  • 做预算查价格的网站是哪个无锡百度推广公司哪家好
  • 东营seo网站建设费用seo兼职平台
  • 微信分享 淘宝网站 怎么做济南做网站比较好的公司
  • 西宁网站建设开发百度推荐现在为什么不能用了
  • 如何用vps做网站常用的网络营销方法及效果
  • 网站 服务报价房地产新闻最新消息
  • 网站运营需要多少钱世界杯排名