创建并移动到新目录[重复]

问题描述 投票:0回答:1

我正在编写一个程序,要求用户输入新文件夹的名称。然后它创建路径并移动到它以保存一些数据。

我有询问和创建路径的代码,但不能完全正确地移动到新的相对位置。

new_dir = input('Enter the directory where you want to save your data :').lower()
if not os.path.exists(new_dir):
    os.makedirs(new_dir)

它创建了路径,但我仍在将数据集保存到 cwd

python directory working-directory
1个回答
0
投票

我刚刚浏览了一些答案,发现有一个名为“shutil”的库具有“move()”函数。你可以试试

import shutil
shutil.move("path/to/current/file.foo", "path/to/new/destination/for/file.foo")

有关更详细的答案,请参阅如何在 Python 中移动文件。希望这能满足您的需求。

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.