使用shutil模块将文件移动到当前目录

发布于 2024-12-09 10:05:40 字数 281 浏览 4 评论 0原文

我知道这可能听起来很愚蠢,但是如何将用户浏览到的目录(我命名为我的 filedir)中的文件移动到我所在的当前目录?

例如:我在“C:\webs”中有一个名为“pages.html”的文件。如何将该文件移动到当前工作目录“.”?

这是我的代码:

shutil.move(filedir, "*.*")
#I got errors using this code.. 

除了“.”之外,还有其他方式表示当前目录吗?

I know this may sound really stupid, but how can I move a file in a directory a user browsed to ( I named mine filedir) to the current directory I am in?

for example: I have a file called "pages.html" in "C:\webs". How can I move that file to the current working directory "."?

This is my code:

shutil.move(filedir, "*.*")
#I got errors using this code.. 

Is there another way to say current directory, other than "." ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美男兮 2024-12-16 10:05:40

shutil.move 的第二个参数指定目录,而不是全局掩码:

import os.path
shutil.move(os.path.join(filedir, "pages.html"), os.getcwd())

应该可以。

The second argument of shutil.move specifies a directory, not a glob mask:

import os.path
shutil.move(os.path.join(filedir, "pages.html"), os.getcwd())

should work.

树深时见影 2024-12-16 10:05:40

如果您发布错误消息和完整的回溯,将会非常有帮助。但是,要获取当前工作目录,您可以使用 os.getcwd()。只要 filedir 指向一个文件而不是目录,这应该可以工作。

filedir = r"C:\webs\pages.html"
shutil.move(filedir, os.getcwd())

It would be very helpful if you posted the error message and the complete traceback. However, to get the current working directory you can use os.getcwd(). As long as filedir points to a file and not the directory this should work.

filedir = r"C:\webs\pages.html"
shutil.move(filedir, os.getcwd())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文