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

dede后台做两个网站建设mylove卡网站

dede后台做两个网站,建设mylove卡网站,最新经济新闻头条,北京中航空港建设工程有限公司网站接上节课内容#xff0c;在开始正式移动文件到目标文件夹之前#xff0c;我们需要再思考一个问题。在代码运行之前#xff0c;阿文的下载文件夹里已经存在一些分类文件夹了#xff0c;比如图例中“PDF文件”这个文件夹就是已经存在的。这样的话#xff0c;在程序运行时在开始正式移动文件到目标文件夹之前我们需要再思考一个问题。在代码运行之前阿文的下载文件夹里已经存在一些分类文件夹了比如图例中“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.w-s-a.com/news/199850/

相关文章:

  • 外贸网站模板免费下载微网站制作软件
  • 一个新的网站怎么做宣传技术先进的网站建
  • 福建网站建设有限公司需求网站
  • 生物科技企业网站做的比较好的企业培训网站模板
  • 南京 高端网站建设网站备案密码怎么找回
  • 蛋糕店网站模板建设电子商务系统网站
  • 海口网站建设优化公司网站开发要加班吗
  • 建设一个网站需要注意哪些要求群晖的网站开发
  • 精通网站开发阅读网页视频下载慢怎么办
  • 网站标题的选择巴音郭楞库尔勒网站建设
  • 成都市网站建设服务商怎么进网站后台管理系统
  • 企业网站站内优化30岁做网站编辑
  • 与安网站建设网站建设征求意见稿
  • 学校网站推广做网站手把手
  • 网站开发遇到的难题wordpress文章调用
  • 网站建设做的好在线logo免费设计生成器标智客
  • 郑州做网站齿轮网站排名怎么做 site
  • 船员专用网站开发建议wordpress 图片占用id
  • 桌面软件开发跟网站开发那个上海网站备案在哪里查询
  • 罗湖网站建设陕西省建设网官网住房和城乡厅官网
  • 做一个网站的详细教学wordpress 忘记用户名密码
  • 建设银行的网站是多少wordpress添加新页面跳转
  • 网站设计费用抓取网站访客qq号码
  • 驾校视频网站模板郑州网站建设价格
  • 上海 有哪些做网站的公司成都企业网站备案流程
  • 移动端快速建站的方法青海公司网站建设
  • 网站重新搭建程序要多少钱移动互联网应用的使用情况
  • 学建站论坛给别人做网站怎么赚钱吗
  • 响应式网站代码校友会网站建设的目的
  • 北京公司网站网站建设html模板