Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Shutil-DS_存储文件阻塞移动问题_Python_Shutil - Fatal编程技术网

Python Shutil-DS_存储文件阻塞移动问题

Python Shutil-DS_存储文件阻塞移动问题,python,shutil,Python,Shutil,新的Python开发人员正在尝试编写我的第一个脚本。目标是编写一个应用程序,根据文件类型将文件放入子文件夹,从而清理我的下载文件夹 这是我的密码: 导入shutil 导入操作系统 directory=“/Users/Gustaf/downloads” 文件类型列表=[] 对于os.listdir中的文件名(“/Users/Gustaf/downloads”):#插入下载文件夹路径 路径=目录 file_type=os.path.splitext(文件名)[1] 如果文件类型不在文件类型列表中:

新的Python开发人员正在尝试编写我的第一个脚本。目标是编写一个应用程序,根据文件类型将文件放入子文件夹,从而清理我的下载文件夹

这是我的密码:

导入shutil
导入操作系统
directory=“/Users/Gustaf/downloads”
文件类型列表=[]
对于os.listdir中的文件名(“/Users/Gustaf/downloads”):#插入下载文件夹路径
路径=目录
file_type=os.path.splitext(文件名)[1]
如果文件类型不在文件类型列表中:
文件类型列表。追加(文件类型)
如果文件类型列表中的文件类型:
持续
尝试:
打印(目录+文件类型。替换(“.”,“/”)
mkdir(目录+文件类型.replace(“.”,“/”)
除OSError as错误外:
打印(错误)
对于os.listdir中的文件名(“/Users/Gustaf/downloads”):
可移动的文件路径=目录+“/”+“%s”%(文件名)
file_type=os.path.splitext(文件名)[1]
文件类型扩展名=文件类型。替换(“.”,“”)
file_no_extension=os.path.splitext(文件名)[0]#用于shutil.move中的完整文件路径
fileDestination=目录+“/”+“%s”%(文件类型\无扩展名)
如果os.path.isdir(可移动文件路径)==True:
#跳过目录
打印(可移动的文件路径)
打印(“这是一个文件夹”+“\n”)
如果os.path.isfile(movable_file_path)==True:
#实际要移动的文件
打印(文件名)
打印(“这是一个文件”+“\n”)
移动(可移动的文件路径,文件目的地)
第一部分工作正常,程序会为每种文件类型创建文件夹,但当我尝试移动文件时,会得到以下结果:

Traceback (most recent call last):
  File "/Users/Gustaf/Desktop/Programming/downloads_sorter/main.py", line 41, in <module>
    shutil.move(movable_file_path, fileDestination) 
  File "/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/shutil.py", line 786, in move
    raise Error("Destination path '%s' already exists" % real_dst)
shutil.Error: Destination path '/Users/Gustaf/downloads/.DS_Store' already exists
第一个是导致问题的问题(DS_store)。有些文件可以移动,但遇到这种情况后,我什么也找不到。我做错了什么?

如下所示:

  • 文件名:
    DS\u存储
  • 可移动文件路径:
    /Users/Gustaf/downloads/DS\u Store
  • 目的地:
    /Users/Gustaf/downloads/
您的代码正在尝试将DS_存储从
/Users/Gustaf/downloads/DS_Store
/Users/Gustaf/downloads/DS_Store
,实际上是同一个地方。您根本没有尝试移动DS_商店,因此应该忽略它。您可以通过检查
可移动文件路径中是否存在
文件名

在这种情况下,您可以按如下方式更改if语句:

if os.path.isfile(movable_file_path) and filename not in movable_file_path:
    #The files you actually want to move
    print(filename)
    print("THIS IS A FILE" + "\n")
    shutil.move(movable_file_path, fileDestination) 
92722314_10157775412048005_7592678894226898944_n.jpg
/Users/Gustaf/downloads/92722314_10157775412048005_7592678894226898944_n.jpg
/Users/Gustaf/downloads/jpg
epub-download-atomic-habits-by-james-clear-9781847941831-fhy.epub
/Users/Gustaf/downloads/epub-download-atomic-habits-by-james-clear-9781847941831-fhy.epub
/Users/Gustaf/downloads/epub
if os.path.isfile(movable_file_path) and filename not in movable_file_path:
    #The files you actually want to move
    print(filename)
    print("THIS IS A FILE" + "\n")
    shutil.move(movable_file_path, fileDestination)