简体   繁体   English

python 将文件从一个目录移到另一个目录

[英]python remove file from one directory to another

I was trying to move the files from a folder to another one on my local machine using shutil.move .我试图使用shutil.move将文件从一个文件夹移动到本地计算机上的另一个文件夹。 But I kept getting this error:但我一直收到这个错误: 在此处输入图像描述

I think it occurs because the destination does not exist because it is exactly where I going to move it.我认为它发生是因为目的地不存在,因为它正是我要移动它的地方。 So how can I make the file existent before moving it there?那么在将文件移到那里之前如何使文件存在呢?

Note: here is my codes:注意:这是我的代码:

import os
import shutil

path = '.\PDF_data\PDF'
record = pd.read_csv('~/Desktop/sec_results1.csv')
for file in tqdm(record['ID Number']):
    pdf = path + '/' + file + '.pdf'
    if os.path.exists(pdf):
        shutil.move(pdf, '~/Desktop/PDF_extracted' + '/' + file + '.pdf')

If '~/Desktop/PDF_extracted' isn't a directory that already exists, you'll have to create it prior to moving files there.如果'~/Desktop/PDF_extracted'不是一个已经存在的目录,您必须在将文件移动到那里之前创建它。 You can do so using os.mkdir or pathlib.Path.mkdir .您可以使用os.mkdirpathlib.Path.mkdir这样做。

The following is how you could accomplish this with either one:以下是您如何使用其中任何一个来完成此操作:

os.mkdir操作系统文件目录

fpath = '~/Desktop/PDF_extracted'
if not os.path.exists(fpath):
    os.mkdir(fpath)

pathlib.Path.mkdir路径库.Path.mkdir

from pathlib import Path 

fpath = '~/Desktop/PDF_extracted'
path_obj = Path(fpath)
if not path_obj.exists():
    path_obj.mkdir()

Side note:边注:

Working with filepath's can be tricky so I would definitely recommend looking into some of os and especially pathlib 's filepath manipulation methods, they make life a lot easier and will greatly reduce confusion when performing tasks like this.使用文件路径可能很棘手,所以我肯定会建议研究一些os ,尤其是pathlib的文件路径操作方法,它们使生活变得更加轻松,并且在执行此类任务时会大大减少混淆。

Additional resources:额外资源:

import os
import shutil
path = '.'
print(os.path.abspath(path))
os.mkdir("new_folder")
old_folder = "test"
shutil.move("~/Desktop/test/file.txt", "new_folder")
  1. You are in the current directory indicated by '.'您位于“.”指示的当前目录中period sign and if you want to know your absolute path directory.句号,如果你想知道你的绝对路径目录。
  2. You make 'new_folder' by 'os.mkdir' code您通过“os.mkdir”代码创建“new_folder”
  3. Assuming that you have 'test' folder in your current directory '~/Desktop' containing 'file.txt' file to move.假设您的当前目录“~/Desktop”中有“test”文件夹,其中包含要移动的“file.txt”文件。
  4. 'shutil.move' will help you to move 'file.txt' file from '~/Desktop/test/' folder to '~/Desktop/new_folder' folder. 'shutil.move' 将帮助您将 'file.txt' 文件从 '~/Desktop/test/' 文件夹移动到 '~/Desktop/new_folder' 文件夹。

I did it on Windows 10 so ~\Desktop can not be recognized.我在 Windows 10 上做的,所以~\Desktop不能被识别。 I need to change it into a Microsoft type such as ./../Desktop .我需要将其更改为 Microsoft 类型,例如./../Desktop

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python - 一个目录中的嵌套文件导入另一个目录中的嵌套文件 - Python - nested file in one directory importing nested file in another directory 将文件从一个目录复制到另一个python - Copying files from one directory to another python 如何将文件从一个目录移动到另一个目录? - How to move a file from one directory to another? 如何从另一个目录导入python文件 - how to import python file from another directory 从 Python 中的另一个目录加载 JSON 文件 - Loading JSON file from another directory in Python Python从另一个目录调用文件 - Python calling a file from another directory 如果文件出现在Python的文本文件中,则将文件从一个目录移动到另一个目录(请参阅2) - Move files from one directory to another if they appear in a text file in Python (take 2) databricks python dbutils 无法将文件从一个目录移动到另一个目录 - databricks python dbutils can't move file from one directory to another Python 脚本将文件和目录从一台远程服务器复制到另一台远程服务器 - Python script to copy file and directory from one remote server to another remote server python - 仅当目标中不存在文件时,如何将文件从一个目录复制到另一个目录? - python - How to copy files from one directory to another only if the file is not present in destination?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM