开发者

move a file to the current directory using shutil module

开发者 https://www.devze.com 2023-04-12 19:41 出处:网络
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 curren开发者_运维技巧t directory I am in?

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 curren开发者_运维技巧t 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 "." ?


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.


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())
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号