python - 将文件移动到另一个文件夹的问题
问题描述
我有这个代码可以将一个项目移动到另一个文件夹中
import os
files = os.listdir('C:/Users/EL127032/youtube-downloader-converter')
print(files)
numbertocheck = 0
numberoftimes = len(files)
while numbertocheck != numberoftimes:
if any("mp3" in s for s in files):
filechecking = files[numbertocheck]
if "mp3" in filechecking():
oldfilepath = ("C:/Users/EL127032/youtube-downloader-converter/" + filechecking)
newfilepath = ("C:/MuziekMP3/" + filechecking)
os.rename(oldfilepath, newfilepath)
numbertocheck = numbertocheck + 1
但是当我运行这个时,我得到
File "C:\Users\EL127032\PycharmProjects\pythonProject2\main.py", line 12, in <module>
if "mp3" in filechecking():
TypeError: 'str' object is not callable
解决方案
在第 12 行(根据错误消息的建议),您有:
...
if "mp3" in filechecking():
...
后面的括号filechecking
将使解释器假定它是一个需要调用的函数。
但是,filechecking
是一个str
(字符串),其中包含您感兴趣的文件名。
只需删除()
或将其更改为:
...
if filechecking.endswith(".mp3"):
...
然后再试一次。
推荐阅读
- python - pymongo insert_many 改变现有文档
- asp.net - mscorlib.dll 中出现“System.AggregateException”类型的异常,但未在用户代码中处理
- java - 由于 JPA 中的延迟/急切加载,三个实体映射无法正常工作
- spring - 持续向 AWS 托管的 Spring Boot 应用程序部署功能
- c# - 在响应标头中传递非 ascii 字符
- random - 在 0 到 1 GLSL ES 之间生成一个伪随机数
- javascript - 在 HTML 中解析 JavaScript
- pandas - 如何在多索引列上使用 pandas rename()?
- angular - 如何将额外参数传递给 RxJS 映射运算符
- sql - SQL聚合同一天/周内特定时间间隔的分钟总和(电话状态)