0

See the following code:

def increment(): x = 0 file = open(f'file{x}.txt', 'a') file.write("something...") file.close() x = x + 1 

You might have figured out what, I'm trying to do here. But the problem is, each time I run the program the value of x is set to 0 and every time it's opening the file, file1.txt, I even tried this code:

x = 0 def increment(): file = open(f'file{x}.txt', 'a') file.write("something...") file.close() x = x + 1 

Assigning the value outside the function, but still the same problem. I want it to open files like this:
First Run: file1.txt,
Second Run: file2.txt,
Third Run: file3.txt and so on...
But it's always opening the file, file1.txt.

5
  • 1
    Make a minimal reproducible example. In your second attempt, if you call the function you get a specific error. Look up what that error means or just search that error on SO. Commented Jan 22, 2022 at 7:24
  • "You might have figured out what, I'm trying to do here.", is a very vague statement, any question on SO, must include all information about the problem you are facing including what you are trying to accomplish from the problematic line of code(if not the whole project..). As suggested by @MarkTolonen please provide a minimal reproducible example. Commented Jan 22, 2022 at 7:28
  • Well, how do you want the program to figure out this information? Should it look at the existing files and figure out which number comes next? Should it read a number that you stored somewhere? (If so, where and how?) Something else? Commented Jan 22, 2022 at 7:29
  • 1
    Please read How to Ask and meta.stackoverflow.com/questions/284236. Commented Jan 22, 2022 at 7:31
  • Obviously you need to store x value and read it at the start of the program then save the new value at the end. There are plenty of possible options how to do that. Commented Jan 22, 2022 at 7:42

2 Answers 2

2

variables are stored in memory, not hard drive. whenever your program ends and the process terminates, Python will automatically remove all the references to created objects in memory therefore all the objects will go...

In order to do that, you have 2 choices:

solution 1:

you need to somehow store that number in hard drive. May be in a separate text file. Then whenever you want to create new files, first read that file and get that number from it. Do your job and at the end do not forget to write it back.

like:

# getting the number (assumed this file is already present # and has the first line filled with a number) with open("read_number.txt") as f: x = int(f.read()) with open(f"file{x}.txt", 'a') as f: f.write("something\n") with open("read_number.txt", 'w') as f: x += 1 f.write(str(x)) 

solution 2:

Another solution is whenever you want to create a new file, search the directory , get all the names(with os.listdir) and sort them. Then you can get the last name and can easily increment it by one.

something like:

import os import re lst = [i for i in os.listdir() if i.startswith("file")] lst.sort(key=lambda x: int(re.search(r'\d+', x).group())) print(lst) 

You can do this in many ways I just intended to show you the path.

Sign up to request clarification or add additional context in comments.

Comments

0

If what you are trying to accomplish here, is saving files with incremented numbers, then one approach can be to start from x = 0, every time the program is run and then check if a file with the name file{x} exists, if it does then increment and check again, and if it does not then simply use that filename.

Like so -:

from os import path def increment() : x = 0 while path.exists('file' + str(x)) : continue file_name = 'file' + str(x) with open(file_name) as f : #.... pass return 

Here, to check for whether a file exists or not, the exists method of the os.path module can be used.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.