首页 > 解决方案 > 将文件移动到另一个文件夹的问题

问题描述

我有这个代码可以将一个项目移动到另一个文件夹中

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

标签: pythonstringoperating-systemcallable

解决方案


在第 12 行(根据错误消息的建议),您有:

...
if "mp3" in filechecking():
...

后面的括号filechecking将使解释器假定它是一个需要调用的函数。

但是,filechecking是一个str(字符串),其中包含您感兴趣的文件名。

只需删除()或将其更改为:

...
if filechecking.endswith(".mp3"):
...

然后再试一次。


推荐阅读