I
I
InternetMaster2021-10-18 19:22:27
Python
InternetMaster, 2021-10-18 19:22:27

How to pull the file with the maximum number?

Task: there is a folder with text data .txt, where all the names are ordered like: "1.txt", "2.txt", "3.txt" .... "15.txt", "16.txt" and so on .d. How to take a file with the last (maximum) number and create a file with the next number and work with it already. For example, if the maximum is "25.txt", then you need to create "26.txt" and work with it already. How to implement this in Python with open()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Dugin, 2021-10-18
@InternetMaster

from pathlib import Path

max_id = max(int(path.stem) for path in Path('path_to_folder').glob('*.txt'))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question