【发布时间】:2013-03-02 00:34:37
【问题描述】:
我正在尝试用 Python 编写一个简单的程序,该程序从我的下载文件夹中获取所有音乐文件并将它们放入我的音乐文件夹中。我使用的是 Windows,可以使用 cmd 提示符移动文件,但出现此错误:
WindowsError: [Error 2] The system cannot find the file specified
这是我的代码:
#! /usr/bin/python
import os
from subprocess import call
def main():
os.chdir("C:\\Users\Alex\Downloads") #change directory to downloads folder
suffix =".mp3" #variable holdinng the .mp3 tag
fnames = os.listdir('.') #looks at all files
files =[] #an empty array that will hold the names of our mp3 files
for fname in fnames:
if fname.endswith(suffix):
pname = os.path.abspath(fname)
#pname = fname
#print pname
files.append(pname) #add the mp3 files to our array
print files
for i in files:
#print i
move(i)
def move(fileName):
call("move /-y "+ fileName +" C:\Music")
return
if __name__=='__main__':main()
我查看了subprocess 库和无数其他文章,但我仍然不知道我做错了什么。
【问题讨论】:
-
您的
fileName不是包含空格吗?如果是,则必须改用'"' + fileName + '"',否则move将找不到该文件。 -
在旁注中,这些不是数组,而是列表
-
[os.rename](http://docs.python.org/2/library/os.html#os.rename)有什么问题
标签: python subprocess windowserror