简体   繁体   English

将文件从一个目录复制到另一个python

[英]Copying files from one directory to another python

Hello does anyone know how to copy a file from one directory to another? 您好,有人知道如何将文件从一个目录复制到另一个目录吗? I have used the "shutil.copy2" it works and gets the copies to the specified output. 我已经使用了“ shutil.copy2”,它可以工作并将副本复制到指定的输出。

Though, my goal is being able to copy files from one directory to another, allowing users to specify which files they want to copy by name. 但是,我的目标是能够将文件从一个目录复制到另一个目录,从而允许用户通过名称指定要复制的文件。 Rather than having to input the dir path every time. 不必每次都输入目录路径。

Thought process: Since I specify the file directory. 思考过程:由于我指定了文件目录。 Somehow using a raw_input user can specify which file they want to copy from the specified directory. 使用raw_input用户可以通过某种方式指定要从指定目录复制的文件。 Posted my code for reference. 发表我的代码以供参考。 #Please not BS comments I am new to coding, just trying to learn. #请不要BS评论我是编码新手,只是想学习。

#----------------------------------------------------------------------------------------------------------------#
# These params will be used for specifying which template you want to copy and where to output 
#----------------------------------------------------------------------------------------------------------------#
'''Load file from x directory into current working directory '''

#PullTemplate: Specify which template you want to copy, by directory path

TemplateRepo = ("/home/hadoop/BackupFolders/Munge_Stage_Templates/Templates")

user_input = raw_input("which file do you want to pull:")


#OutputTemplate: Let's you specify where you want to output the copied template.
#Originally set to your current working directory (u".")

OutputTemplate = (u".")


#----------------------------------------------------------------------------------------------------------------#
# STATIC CODE: Do not alter "Just Run!"
#----------------------------------------------------------------------------------------------------------------#
shutil.copy2(TemplateRepo, OutputTemplate)

So a bit of clarification are you trying to have them just enter a filename or are you asking how to get a relative path? 因此,您需要使它们只是输入文件名还是在询问如何获取相对路径?

EDIT 编辑

Ok so for starters to make you life easier now and in the future use a function will make your life alot easier. 好的,这样对于初学者来说,现在和将来使您的生活变得更轻松,函数将使您的生活更加轻松。 Second check out the docs https://docs.python.org/3/library/functions.html#open great resource and most things you want to do it will tell you how. 其次,查看docs https://docs.python.org/3/library/functions.html#open优质资源,您要做的大多数事情都会告诉您如何使用。 You will have to use the absolute path for saving the file but you can use a relative one for opening it. 您必须使用绝对路径保存文件,但是可以使用相对路径打开文件。

The first option for shutil.copy2() should be a file instead of a directory, so you would need to add TemplateRepo and the file name from the input: shutil.copy2()的第一个选项应该是文件而不是目录,因此您需要从输入中添加TemplateRepo和文件名:

TemplateRepo = ("/home/hadoop/BackupFolders/Munge_Stage_Templates/Templates")
user_input = raw_input("which file do you want to pull:")
OutputTemplate = (u".")

InputFile = TemplateRepo + '/' + user_input

shutil.copy2(InputFile, OutputTemplate)

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

相关问题 IsADirectoryError: [Errno 21] 是目录:'/' 将文件从一个文件夹复制到另一个现有文件夹时 Python - IsADirectoryError: [Errno 21] Is a directory: '/' while copying files from one folder to another existing folder Python Python - 仅将新文件复制到另一个目录中 - Python - Copying only new files into another directory 使用 python 将文件从服务器的一个位置复制到另一个位置 - Copying files from one location of a server to another using python 只要子目录名称匹配,就将文件从一个目录和子目录复制到另一个目录 - Copying files from one directory, and sub-directories, to another, so long as sub-directory names match 将文件从一个文件夹复制到另一个 - copying files from one folder to another python glob 或 listdir 创建然后将文件从一个目录保存到另一个 - python glob or listdir to create then save files from one directory to another 将某些文件从一个目录移动到另一个目录-Python - Move Certain Files from One Directory to Another - Python 使用python将文件从一个目录转移到另一个目录 - Transfer files from one directory to another if they match a condition using python 使用 Python 将所有文件从一个目录移动到另一个目录 - Moving all files from one directory to another using Python 通过 python 将文件从一个目录传输到另一个目录 - Transfering files from one directory to another via python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM