-1

I'm writing a program that asks the user to input the name of a new folder. then it creates the path and moves to it to save some data.

I have the code to ask and create the path, but can't get it quite right to move to the new relative location.

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)

it creates the path, but I'm still saving dataset to the cwd

Philip
  • 21
  • 2
  • 2
    where is your code to move to that directory and save the data there? [ask] and [mre] – Julien Mar 15 '23 at 04:44
  • 1
    Show the code that saves the data (properly formatted in the question). – Michael Butscher Mar 15 '23 at 04:45
  • 1
    Welcome to Stack Overflow. For future questions, please try to focus on **the question**. Saying things like "but can't get it quite right to move to the new relative location" isn't very helpful - this implies *that the code you're showing includes an attempt to do that*. If you mean that you think the existing code should do that, then you should explicitly ask that: "why doesn't this code have that effect?" If you mean that you just don't know how to do it, ask that instead. – Karl Knechtel Mar 15 '23 at 05:06
  • 1
    But before asking, [please try](https://meta.stackoverflow.com/questions/261592) to look for existing Q&A - for example by [using a search engine](https://duckduckgo.com/?q=python+change+to+a+new+directory). – Karl Knechtel Mar 15 '23 at 05:18

1 Answers1

-2

I just browsed some answers and found that there is a library named 'shutil' which has function 'move()'. You can try

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

for more detailed answer please refer How do I move a file in Python. Hope this meets your needs.

  • 1
    Welcome to Stack Overflow. The goal is not to move a file, but to change the current working directory ("move to" that folder, so that future operations will take place there). But aside from that, if you think a question already has a "more detailed answer" somewhere else on Stack Overflow, **please do not answer it** - instead, flag the question as a duplicate, to help close it properly. We do not want answers for duplicate questions on Stack Overflow - we want the relevant information all in the same place. We do not want custom answers that address the OP - we want a searchable library. – Karl Knechtel Mar 15 '23 at 05:19
  • 1
    Please read [answer] to understand the policy properly. – Karl Knechtel Mar 15 '23 at 05:20
  • Okay. I will try to improve the answer, if possible or will delete it. Thanks for helping. – Narayan Vaze Mar 15 '23 at 07:02