Relocate the file with the files to another file. How better?



  • There's a structure for directors:

       test_0    
        |-----level_1
        |-----level_1_a
        |-----level_1_b
        | ... (и т.д.)  ....      
        |-----task     
               |----file.csv   
               |----rest.py     
                ... (и т.д.)  ....
    

    We need to move the file file to the folded file. I mean, for example, level_1:

       test_0    
        |-----level_1
            |-----task     
                   |----file.csv   
                   |----rest.py     
                   ... (и т.д.)  ....
        |-----level_1_a
        |-----level_1_b
        | ... (и т.д.)  ....      
    

    Well, here's the code,

    from pathlib import Path
    

    work_dir = Path() / "test_0"
    task_dir = work_dir / "task"

    folder_move_to = work_dir / "level_1" / "task"
    folder_move_to.mkdir()

    for file in task_dir.iterdir():
    file.rename(work_dir / "level_1" / "task" / file.name)

    task_dir.rmdir()

    Similar options are given, for example https://stackoverflow.com/questions/8858008/how-to-move-a-file ♪
    It's almost okay. But there's still a feeling that it's kind of heavy. Must be better.

    Question: What is the best way to move the folder in this case,
    safer, shorter...



  • Anyway, in my situation, when everyone on the same disc can just rename the CD file. Then she'll move in with the files.

    I mean, instead.

    for file in task_dir.iterdir():
        file.rename(work_dir / "level_1" / "task" / file.name)
    

    sufficient renegotiation.

     task_dir.rename(work_dir / "level_1" / "task")
    

    Bingo!
    Thank you.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2