0

I would like to write files of the same name to a directory using Python. Like when downloading the same file from a browser.

I have an existing directory containing the files "aaa.py" and "aaa(1).py".

I would like to add several versions of a file named "aa.py".

The resulting directory should contain: "aaa.py" "aaa(1).py" "aa.py" "aa(1).py" "aa(2).py" etc.

user1530405
  • 447
  • 1
  • 8
  • 16
  • 1
    Maybe try looking if a file `aa` exists, and if it does renaming the moved file to `aa(1)`? Same logic for `aa(2)`, `aa(3)` etc. – Codeman Feb 17 '22 at 23:40
  • 1
    You might find the [`pathlib`](https://docs.python.org/3/library/pathlib.html) module very helpful for your need, especially the function [`Path.exists`](https://docs.python.org/3/library/pathlib.html#pathlib.Path.exists) and [`Path.touch`](https://docs.python.org/3/library/pathlib.html#pathlib.Path.touch). – Fanchen Bao Feb 17 '22 at 23:57

0 Answers0