PYTHON |从未知目的地复制文件 [英] PYTHON | Copying a file from an unknown destination

查看:52
本文介绍了PYTHON |从未知目的地复制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我叫Oscar,希望获得您的帮助!因此,我遇到的情况是我试图编写一个Python代码,该代码将自身复制并将该副本放置在另一个目录中.我知道您可以使用shutil命令.例如:

Hi my name is Oscar and I would like some help! So the scenario I have is that I am trying to write a python code that will copy itself and put that copy in another directory. I know that you can use the shutil command. For example:

导入关闭shutil.copy("C:\ Users \%user%\ Downloads \ file.txt","C:\ Users \%user%\ Documents")

import shutil shutil.copy("C:\Users\%user%\Downloads\file.txt", "C:\Users\%user%\Documents")

但是我只想复制文件,以便脚本可以在它所在的任何计算机上运行,​​例如:

But I want to just copy the file so that the script works on any computer that it is located on, for example:

导入关闭shutil.copy("file.txt","C:\ Users \%user%\ Documents")

import shutil shutil.copy("file.txt", "C:\Users\%user%\Documents")

如果您不明白我的意思,请询问,我会尽力详细说明.

If you don't understand what I mean please ask and I can try to elaborate.

此致,奥斯卡奖:)

我似乎很笨,我不知道您有时必须使用反斜杠键入目标文件夹才能真正使其成为路径.一切都解决了,为浪费大家的时间而感到抱歉.

I am super dumb it seems like, I did not know that you had to type the destination folder with to backslashes sometimes to actually make it a path. All solved, sorry for wasting everyone's time.

推荐答案

您可以使用pythons标准的"os"库来制作与操作系统无关的文件路径.然后,您可以使用任何已执行脚本中可用的特殊"文件"变量来获取已执行脚本的位置,以将其自身复制到某个目标位置.祝你好运!

You could use pythons standard "os" library to make OS independent filepaths. Then you use the special "file" variable available inside any executed script to get the location of the executed script, to copy itself to some target location. Good luck!

这篇关于PYTHON |从未知目的地复制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆