How do I create a incrementing filename in Python?












20















I'm creating a program that will create a file and save it to the directory with the filename sample.xml. Once the file is saved when i try to run the program again it overwrites the old file into the new one because they do have the same file name. How do I increment the file names so that whenever I try to run the code again it will going to increment the file name. and will not overwrite the existing one. I am thinking of checking the filename first on the directory and if they are the same the code will generate a new filename:



fh = open("sample.xml", "w")
rs = [blockresult]
fh.writelines(rs)
fh.close()









share|improve this question





























    20















    I'm creating a program that will create a file and save it to the directory with the filename sample.xml. Once the file is saved when i try to run the program again it overwrites the old file into the new one because they do have the same file name. How do I increment the file names so that whenever I try to run the code again it will going to increment the file name. and will not overwrite the existing one. I am thinking of checking the filename first on the directory and if they are the same the code will generate a new filename:



    fh = open("sample.xml", "w")
    rs = [blockresult]
    fh.writelines(rs)
    fh.close()









    share|improve this question



























      20












      20








      20


      12






      I'm creating a program that will create a file and save it to the directory with the filename sample.xml. Once the file is saved when i try to run the program again it overwrites the old file into the new one because they do have the same file name. How do I increment the file names so that whenever I try to run the code again it will going to increment the file name. and will not overwrite the existing one. I am thinking of checking the filename first on the directory and if they are the same the code will generate a new filename:



      fh = open("sample.xml", "w")
      rs = [blockresult]
      fh.writelines(rs)
      fh.close()









      share|improve this question
















      I'm creating a program that will create a file and save it to the directory with the filename sample.xml. Once the file is saved when i try to run the program again it overwrites the old file into the new one because they do have the same file name. How do I increment the file names so that whenever I try to run the code again it will going to increment the file name. and will not overwrite the existing one. I am thinking of checking the filename first on the directory and if they are the same the code will generate a new filename:



      fh = open("sample.xml", "w")
      rs = [blockresult]
      fh.writelines(rs)
      fh.close()






      python file-io






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Apr 2 '17 at 11:11









      Martin Thoma

      43.4k61307532




      43.4k61307532










      asked Aug 1 '13 at 3:31









      Oliver Ven QuilnetOliver Ven Quilnet

      106114




      106114
























          10 Answers
          10






          active

          oldest

          votes


















          38














          I would iterate through sample[int].xml for example and grab the next available name that is not used by a file or directory.



          import os

          i = 0
          while os.path.exists("sample%s.xml" % i):
          i += 1

          fh = open("sample%s.xml" % i, "w")
          ....


          That should give you sample0.xml initially, then sample1.xml, etc.



          Note that the relative file notation by default relates to the file directory/folder you run the code from. Use absolute paths if necessary. Use os.getcwd() to read your current dir and os.chdir(path_to_dir) to set a new current dir.






          share|improve this answer





















          • 1





            Kindly asking what is non-useful or unconstructive here? Voting down without leaving a (constructive) comment seems to be more unconstructive to me.

            – bossi
            Aug 1 '13 at 5:18











          • isfile() is not correct: a directory will match. You want exists() instead, but this is @Eiyrioü von Kauyf's answer. Furthermore, relative paths are not exactly "relative to the directory where the code is run from". They are instead more generally relative to the "current directory" (which is by default the directory that the code is run from). The current directory can be changed within the program, for instance.

            – Eric O Lebigot
            Aug 1 '13 at 5:24













          • The fact that os.path.isfile() matches directories is new to me (and doesn't do as you describe for me on Python 3.3/win), isn't that why there is os.path.isdir() in place to differentiate between the two? In regards to the comment in my post towards the relative path notation neither Oliver Ven Quilnet's nor my example explicitly changes the current directory and I thought I briefly point it out to make it clear for the given context.

            – bossi
            Aug 1 '13 at 5:48






          • 1





            You are right, I should have been clearer. I meant that isfile() will make your loop exit when the name matches a directory, and your code tries then to open the directory in write mode, which fails with IOError. This is why isfile() is not the correct test, and should be replaced by the exists() of @Eiyrioü von Kauyf. As for relative paths, I really think that the current "the relative file notation always relates to the file directory/folder you run the code from" is misleading (because of "always").

            – Eric O Lebigot
            Aug 1 '13 at 6:41













          • @EOL: That's a good point, I honestly wasn't aware that identical names between a file and a folder in the same directory are illegal under Windows; thanks for pointing that out. I agree with you, the remark about the relative path did sound misleading, it should sound clearer now.

            – bossi
            Aug 1 '13 at 7:52



















          10














          def get_nonexistant_path(fname_path):
          """
          Get the path to a filename which does not exist by incrementing path.

          Examples
          --------
          >>> get_nonexistant_path('/etc/issue')
          '/etc/issue-1'
          >>> get_nonexistant_path('whatever/1337bla.py')
          'whatever/1337bla.py'
          """
          if not os.path.exists(fname_path):
          return fname_path
          filename, file_extension = os.path.splitext(fname_path)
          i = 1
          new_fname = "{}-{}{}".format(filename, i, file_extension)
          while os.path.exists(new_fname):
          i += 1
          new_fname = "{}-{}{}".format(filename, i, file_extension)
          return new_fname


          Before you open the file, call



          fname = get_nonexistant_path("sample.xml")


          This will either give you 'sample.xml' or - if this alreay exists - 'sample-i.xml' where i is the lowest positive integer such that the file does not already exist.



          I recommend using os.path.abspath("sample.xml"). If you have ~ as home directory, you might need to expand it first.



          Please note that race conditions might occur with this simple code if you have multiple instances running at the same time. If this might be a problem, please check this question.






          share|improve this answer

































            4














            Try setting a count variable, and then incrementing that variable nested inside the same loop you write your file in. Include the count loop inside the name of the file with an escape character, so every loop ticks +1 and so does the number in the file.



            Some code from a project I just finished:



            numberLoops = #some limit determined by the user
            currentLoop = 1
            while currentLoop < numberLoops:
            currentLoop = currentLoop + 1

            fileName = ("log%d_%d.txt" % (currentLoop, str(now())))


            For reference:



            from time import mktime, gmtime

            def now():
            return mktime(gmtime())


            which is probably irrelevant in your case but i was running multiple instances of this program and making tons of files. Hope this helps!






            share|improve this answer





















            • 2





              Python has for loops for this, they are much faster to read and comprehend than the while loops that emulate them. Furthermore, the % operator is deprecated. No downvote, though, because it does the job—it just does not do it in the preferred Python way.

              – Eric O Lebigot
              Aug 1 '13 at 5:19













            • There is a problem with your format string: you format a string with %d, and this raises an exception.

              – Eric O Lebigot
              Aug 1 '13 at 5:31











            • Thanks for catching that. It should be a %s, I retyped this rather hastily instead of copying from my source. Thanks!

              – ford
              Aug 1 '13 at 5:36



















            4














            Sequentially checking each file name to find the next available one works fine with small numbers of files, but quickly becomes slower as the number of files increases.



            Here is a version that finds the next available file name in log(n) time:



            import os

            def next_path(path_pattern):
            """
            Finds the next free path in an sequentially named list of files

            e.g. path_pattern = 'file-%s.txt':

            file-1.txt
            file-2.txt
            file-3.txt

            Runs in log(n) time where n is the number of existing files in sequence
            """
            i = 1

            # First do an exponential search
            while os.path.exists(path_pattern % i):
            i = i * 2

            # Result lies somewhere in the interval (i/2..i]
            # We call this interval (a..b] and narrow it down until a + 1 = b
            a, b = (i / 2, i)
            while a + 1 < b:
            c = (a + b) / 2 # interval midpoint
            a, b = (c, b) if os.path.exists(path_pattern % c) else (a, c)

            return path_pattern % b


            To measure the speed improvement I wrote a small test function that creates 10,000 files:



            for i in range(1,10000):
            with open(next_path('file-%s.foo'), 'w'):
            pass


            And implemented the naive approach:



            def next_path_naive(path_pattern):
            """
            Naive (slow) version of next_path
            """
            i = 1
            while os.path.exists(path_pattern % i):
            i += 1
            return path_pattern % i


            And here are the results:



            Fast version:



            real    0m2.132s
            user 0m0.773s
            sys 0m1.312s


            Naive version:



            real    2m36.480s
            user 1m12.671s
            sys 1m22.425s


            Finally, note that either approach is susceptible to race conditions if multiple actors are trying to create files in the sequence at the same time.






            share|improve this answer































              1














              Without storing state data in an extra file, a quicker solution to the ones presented here would be to do the following:



              from glob import glob
              import os

              files = glob("somedir/sample*.xml")
              files = files.sorted()
              cur_num = int(os.path.basename(files[-1])[6:-4])
              cur_num += 1
              fh = open("somedir/sample%s.xml" % cur_num, 'w')
              rs = [blockresult]
              fh.writelines(rs)
              fh.close()


              This will also keep incrementing, even if some of the lower numbered files disappear.



              The other solution here that I like (pointed out by Eiyrioü) is the idea of keeping a temporary file that contains your most recent number:



              temp_fh = open('somedir/curr_num.txt', 'r')
              curr_num = int(temp_fh.readline().strip())
              curr_num += 1
              fh = open("somedir/sample%s.xml" % cur_num, 'w')
              rs = [blockresult]
              fh.writelines(rs)
              fh.close()





              share|improve this answer


























              • Your cur_num calculation only works for 1-digit numbers, it is not general enough.

                – Eric O Lebigot
                Aug 1 '13 at 5:21











              • Good point, updated.

                – Vorticity
                Aug 1 '13 at 5:25





















              1














              Another example using recursion



              import os
              def checkFilePath(testString, extension, currentCount):
              if os.path.exists(testString + str(currentCount) +extension):
              return checkFilePath(testString, extension, currentCount+1)
              else:
              return testString + str(currentCount) +extension


              Use:



              checkFilePath("myfile", ".txt" , 0)





              share|improve this answer































                1














                You can use a while loop with a counter which checks if a file with a name and the counter's value exists if it does then move on else break and make a file.



                I have done it in this way for one of my projects:`



                from os import path
                import os

                i = 0
                flnm = "Directory\Filename" + str(i) + ".txt"
                while path.exists(flnm) :
                flnm = "Directory\Filename" + str(i) + ".txt"
                i += 1
                f = open(flnm, "w") #do what you want to with that file...
                f.write(str(var))
                f.close() # make sure to close it.


                `



                Here the counter i starts from 0 and a while loop checks everytime if the file exists, if it does it moves on else it breaks out and creates a file from then you can customize. Also make sure to close it else it will result in the file being open which can cause problems while deleting it.
                I used path.exists() to check if a file exists.
                Don't do from os import * it can cause problem when we use open() method as there is another os.open() method too and it can give the error. TypeError: Integer expected. (got str)
                Else wish u a Happy New Year and to all.






                share|improve this answer
























                • Thanks For Rating!

                  – Matrix Programmer
                  Jan 31 at 10:41



















                0














                The two ways to do it are:




                1. Check for the existence of the old file and if it exists try the next file name +1

                2. save state data somewhere




                an easy way to do it off the bat would be:



                import os.path as pth
                filename = "myfile"
                filenum = 1
                while (pth.exists(pth.abspath(filename+str(filenum)+".py")):
                filenum+=1
                my_next_file = open(filename+str(filenum)+".py",'w')


                as a design thing, while True slows things down and isn't a great thing for code readability





                edited: @EOL contributions/ thoughts



                so I think not having .format is more readable at first glance - but using .format is better for generality and convention so.



                import os.path as pth
                filename = "myfile"
                filenum = 1
                while (pth.exists(pth.abspath(filename+str(filenum)+".py")):
                filenum+=1
                my_next_file = open("{}{}.py".format(filename, filenum),'w')
                # or
                my_next_file = open(filename + "{}.py".format(filenum),'w')


                and you don't have to use abspath - you can use relative paths if you prefer, I prefer abs path sometimes because it helps to normalize the paths passed :).



                import os.path as pth
                filename = "myfile"
                filenum = 1
                while (pth.exists(filename+str(filenum)+".py"):
                filenum+=1
                ##removed for conciseness





                share|improve this answer


























                • The format() method is much more legible than string concatenation, here. I think that the while loop is fine, here. On another topic, why use abspath()?

                  – Eric O Lebigot
                  Aug 1 '13 at 5:23











                • format is more legible, but then he would have to look at string formatting; this is easier to understand on first glance imho. and abspath because i'm ignoring symlinks :/ .... that could lead to confusing errors

                  – Eiyrioü von Kauyf
                  Aug 1 '13 at 5:26













                • While I understand your point, I believe that even beginners should be shown Pythonic examples, so that they take good habits. The behavior of format() is really quite simple to understand and even guess: "{}{}.py".format(filename, filenum). It's even simpler than the algorithm presented here. :)

                  – Eric O Lebigot
                  Aug 1 '13 at 5:29











                • hmm added :) i agree with you on this point.

                  – Eiyrioü von Kauyf
                  Aug 1 '13 at 5:31











                • @EOL whatcha think ;) do I have your approval

                  – Eiyrioü von Kauyf
                  Aug 1 '13 at 5:34





















                0














                Another solution that avoids the use of while loop is to use os.listdir() function which returns a list of all the files and directories contained in a directory whose path is taken as an argument.



                To answer the example in the question, supposing that the directory you are working in only contains "sample_i.xlm" files indexed starting at 0, you can easily obtain the next index for the new file with the following code.



                import os

                new_index = len(os.listdir('path_to_file_containing_only_sample_i_files'))
                new_file = open('path_to_file_containing_only_sample_i_files/sample_%s.xml' % new_index, 'w')





                share|improve this answer































                  0














                  I needed to do something similar, but for output directories in a data processing pipeline. I was inspired by Vorticity's answer, but added use of regex to grab the trailing number. This method continues to increment the last directory, even if intermediate numbered output directories are deleted. It also adds leading zeros so the names will sort alphabetically (i.e. width 3 gives 001 etc.)



                  def get_unique_dir(path, width=3):
                  # if it doesn't exist, create
                  if not os.path.isdir(path):
                  log.debug("Creating new directory - {}".format(path))
                  os.makedirs(path)
                  return path

                  # if it's empty, use
                  if not os.listdir(path):
                  log.debug("Using empty directory - {}".format(path))
                  return path

                  # otherwise, increment the highest number folder in the series

                  def get_trailing_number(search_text):
                  serch_obj = re.search(r"([0-9]+)$", search_text)
                  if not serch_obj:
                  return 0
                  else:
                  return int(serch_obj.group(1))

                  dirs = glob(path + "*")
                  num_list = sorted([get_trailing_number(d) for d in dirs])
                  highest_num = num_list[-1]
                  next_num = highest_num + 1
                  new_path = "{0}_{1:0>{2}}".format(path, next_num, width)

                  log.debug("Creating new incremented directory - {}".format(new_path))
                  os.makedirs(new_path)
                  return new_path

                  get_unique_dir("output")





                  share|improve this answer

























                    Your Answer






                    StackExchange.ifUsing("editor", function () {
                    StackExchange.using("externalEditor", function () {
                    StackExchange.using("snippets", function () {
                    StackExchange.snippets.init();
                    });
                    });
                    }, "code-snippets");

                    StackExchange.ready(function() {
                    var channelOptions = {
                    tags: "".split(" "),
                    id: "1"
                    };
                    initTagRenderer("".split(" "), "".split(" "), channelOptions);

                    StackExchange.using("externalEditor", function() {
                    // Have to fire editor after snippets, if snippets enabled
                    if (StackExchange.settings.snippets.snippetsEnabled) {
                    StackExchange.using("snippets", function() {
                    createEditor();
                    });
                    }
                    else {
                    createEditor();
                    }
                    });

                    function createEditor() {
                    StackExchange.prepareEditor({
                    heartbeatType: 'answer',
                    autoActivateHeartbeat: false,
                    convertImagesToLinks: true,
                    noModals: true,
                    showLowRepImageUploadWarning: true,
                    reputationToPostImages: 10,
                    bindNavPrevention: true,
                    postfix: "",
                    imageUploader: {
                    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                    allowUrls: true
                    },
                    onDemand: true,
                    discardSelector: ".discard-answer"
                    ,immediatelyShowMarkdownHelp:true
                    });


                    }
                    });














                    draft saved

                    draft discarded


















                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f17984809%2fhow-do-i-create-a-incrementing-filename-in-python%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown

























                    10 Answers
                    10






                    active

                    oldest

                    votes








                    10 Answers
                    10






                    active

                    oldest

                    votes









                    active

                    oldest

                    votes






                    active

                    oldest

                    votes









                    38














                    I would iterate through sample[int].xml for example and grab the next available name that is not used by a file or directory.



                    import os

                    i = 0
                    while os.path.exists("sample%s.xml" % i):
                    i += 1

                    fh = open("sample%s.xml" % i, "w")
                    ....


                    That should give you sample0.xml initially, then sample1.xml, etc.



                    Note that the relative file notation by default relates to the file directory/folder you run the code from. Use absolute paths if necessary. Use os.getcwd() to read your current dir and os.chdir(path_to_dir) to set a new current dir.






                    share|improve this answer





















                    • 1





                      Kindly asking what is non-useful or unconstructive here? Voting down without leaving a (constructive) comment seems to be more unconstructive to me.

                      – bossi
                      Aug 1 '13 at 5:18











                    • isfile() is not correct: a directory will match. You want exists() instead, but this is @Eiyrioü von Kauyf's answer. Furthermore, relative paths are not exactly "relative to the directory where the code is run from". They are instead more generally relative to the "current directory" (which is by default the directory that the code is run from). The current directory can be changed within the program, for instance.

                      – Eric O Lebigot
                      Aug 1 '13 at 5:24













                    • The fact that os.path.isfile() matches directories is new to me (and doesn't do as you describe for me on Python 3.3/win), isn't that why there is os.path.isdir() in place to differentiate between the two? In regards to the comment in my post towards the relative path notation neither Oliver Ven Quilnet's nor my example explicitly changes the current directory and I thought I briefly point it out to make it clear for the given context.

                      – bossi
                      Aug 1 '13 at 5:48






                    • 1





                      You are right, I should have been clearer. I meant that isfile() will make your loop exit when the name matches a directory, and your code tries then to open the directory in write mode, which fails with IOError. This is why isfile() is not the correct test, and should be replaced by the exists() of @Eiyrioü von Kauyf. As for relative paths, I really think that the current "the relative file notation always relates to the file directory/folder you run the code from" is misleading (because of "always").

                      – Eric O Lebigot
                      Aug 1 '13 at 6:41













                    • @EOL: That's a good point, I honestly wasn't aware that identical names between a file and a folder in the same directory are illegal under Windows; thanks for pointing that out. I agree with you, the remark about the relative path did sound misleading, it should sound clearer now.

                      – bossi
                      Aug 1 '13 at 7:52
















                    38














                    I would iterate through sample[int].xml for example and grab the next available name that is not used by a file or directory.



                    import os

                    i = 0
                    while os.path.exists("sample%s.xml" % i):
                    i += 1

                    fh = open("sample%s.xml" % i, "w")
                    ....


                    That should give you sample0.xml initially, then sample1.xml, etc.



                    Note that the relative file notation by default relates to the file directory/folder you run the code from. Use absolute paths if necessary. Use os.getcwd() to read your current dir and os.chdir(path_to_dir) to set a new current dir.






                    share|improve this answer





















                    • 1





                      Kindly asking what is non-useful or unconstructive here? Voting down without leaving a (constructive) comment seems to be more unconstructive to me.

                      – bossi
                      Aug 1 '13 at 5:18











                    • isfile() is not correct: a directory will match. You want exists() instead, but this is @Eiyrioü von Kauyf's answer. Furthermore, relative paths are not exactly "relative to the directory where the code is run from". They are instead more generally relative to the "current directory" (which is by default the directory that the code is run from). The current directory can be changed within the program, for instance.

                      – Eric O Lebigot
                      Aug 1 '13 at 5:24













                    • The fact that os.path.isfile() matches directories is new to me (and doesn't do as you describe for me on Python 3.3/win), isn't that why there is os.path.isdir() in place to differentiate between the two? In regards to the comment in my post towards the relative path notation neither Oliver Ven Quilnet's nor my example explicitly changes the current directory and I thought I briefly point it out to make it clear for the given context.

                      – bossi
                      Aug 1 '13 at 5:48






                    • 1





                      You are right, I should have been clearer. I meant that isfile() will make your loop exit when the name matches a directory, and your code tries then to open the directory in write mode, which fails with IOError. This is why isfile() is not the correct test, and should be replaced by the exists() of @Eiyrioü von Kauyf. As for relative paths, I really think that the current "the relative file notation always relates to the file directory/folder you run the code from" is misleading (because of "always").

                      – Eric O Lebigot
                      Aug 1 '13 at 6:41













                    • @EOL: That's a good point, I honestly wasn't aware that identical names between a file and a folder in the same directory are illegal under Windows; thanks for pointing that out. I agree with you, the remark about the relative path did sound misleading, it should sound clearer now.

                      – bossi
                      Aug 1 '13 at 7:52














                    38












                    38








                    38







                    I would iterate through sample[int].xml for example and grab the next available name that is not used by a file or directory.



                    import os

                    i = 0
                    while os.path.exists("sample%s.xml" % i):
                    i += 1

                    fh = open("sample%s.xml" % i, "w")
                    ....


                    That should give you sample0.xml initially, then sample1.xml, etc.



                    Note that the relative file notation by default relates to the file directory/folder you run the code from. Use absolute paths if necessary. Use os.getcwd() to read your current dir and os.chdir(path_to_dir) to set a new current dir.






                    share|improve this answer















                    I would iterate through sample[int].xml for example and grab the next available name that is not used by a file or directory.



                    import os

                    i = 0
                    while os.path.exists("sample%s.xml" % i):
                    i += 1

                    fh = open("sample%s.xml" % i, "w")
                    ....


                    That should give you sample0.xml initially, then sample1.xml, etc.



                    Note that the relative file notation by default relates to the file directory/folder you run the code from. Use absolute paths if necessary. Use os.getcwd() to read your current dir and os.chdir(path_to_dir) to set a new current dir.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Aug 1 '13 at 8:16









                    Eric O Lebigot

                    56.9k36167218




                    56.9k36167218










                    answered Aug 1 '13 at 3:46









                    bossibossi

                    962914




                    962914








                    • 1





                      Kindly asking what is non-useful or unconstructive here? Voting down without leaving a (constructive) comment seems to be more unconstructive to me.

                      – bossi
                      Aug 1 '13 at 5:18











                    • isfile() is not correct: a directory will match. You want exists() instead, but this is @Eiyrioü von Kauyf's answer. Furthermore, relative paths are not exactly "relative to the directory where the code is run from". They are instead more generally relative to the "current directory" (which is by default the directory that the code is run from). The current directory can be changed within the program, for instance.

                      – Eric O Lebigot
                      Aug 1 '13 at 5:24













                    • The fact that os.path.isfile() matches directories is new to me (and doesn't do as you describe for me on Python 3.3/win), isn't that why there is os.path.isdir() in place to differentiate between the two? In regards to the comment in my post towards the relative path notation neither Oliver Ven Quilnet's nor my example explicitly changes the current directory and I thought I briefly point it out to make it clear for the given context.

                      – bossi
                      Aug 1 '13 at 5:48






                    • 1





                      You are right, I should have been clearer. I meant that isfile() will make your loop exit when the name matches a directory, and your code tries then to open the directory in write mode, which fails with IOError. This is why isfile() is not the correct test, and should be replaced by the exists() of @Eiyrioü von Kauyf. As for relative paths, I really think that the current "the relative file notation always relates to the file directory/folder you run the code from" is misleading (because of "always").

                      – Eric O Lebigot
                      Aug 1 '13 at 6:41













                    • @EOL: That's a good point, I honestly wasn't aware that identical names between a file and a folder in the same directory are illegal under Windows; thanks for pointing that out. I agree with you, the remark about the relative path did sound misleading, it should sound clearer now.

                      – bossi
                      Aug 1 '13 at 7:52














                    • 1





                      Kindly asking what is non-useful or unconstructive here? Voting down without leaving a (constructive) comment seems to be more unconstructive to me.

                      – bossi
                      Aug 1 '13 at 5:18











                    • isfile() is not correct: a directory will match. You want exists() instead, but this is @Eiyrioü von Kauyf's answer. Furthermore, relative paths are not exactly "relative to the directory where the code is run from". They are instead more generally relative to the "current directory" (which is by default the directory that the code is run from). The current directory can be changed within the program, for instance.

                      – Eric O Lebigot
                      Aug 1 '13 at 5:24













                    • The fact that os.path.isfile() matches directories is new to me (and doesn't do as you describe for me on Python 3.3/win), isn't that why there is os.path.isdir() in place to differentiate between the two? In regards to the comment in my post towards the relative path notation neither Oliver Ven Quilnet's nor my example explicitly changes the current directory and I thought I briefly point it out to make it clear for the given context.

                      – bossi
                      Aug 1 '13 at 5:48






                    • 1





                      You are right, I should have been clearer. I meant that isfile() will make your loop exit when the name matches a directory, and your code tries then to open the directory in write mode, which fails with IOError. This is why isfile() is not the correct test, and should be replaced by the exists() of @Eiyrioü von Kauyf. As for relative paths, I really think that the current "the relative file notation always relates to the file directory/folder you run the code from" is misleading (because of "always").

                      – Eric O Lebigot
                      Aug 1 '13 at 6:41













                    • @EOL: That's a good point, I honestly wasn't aware that identical names between a file and a folder in the same directory are illegal under Windows; thanks for pointing that out. I agree with you, the remark about the relative path did sound misleading, it should sound clearer now.

                      – bossi
                      Aug 1 '13 at 7:52








                    1




                    1





                    Kindly asking what is non-useful or unconstructive here? Voting down without leaving a (constructive) comment seems to be more unconstructive to me.

                    – bossi
                    Aug 1 '13 at 5:18





                    Kindly asking what is non-useful or unconstructive here? Voting down without leaving a (constructive) comment seems to be more unconstructive to me.

                    – bossi
                    Aug 1 '13 at 5:18













                    isfile() is not correct: a directory will match. You want exists() instead, but this is @Eiyrioü von Kauyf's answer. Furthermore, relative paths are not exactly "relative to the directory where the code is run from". They are instead more generally relative to the "current directory" (which is by default the directory that the code is run from). The current directory can be changed within the program, for instance.

                    – Eric O Lebigot
                    Aug 1 '13 at 5:24







                    isfile() is not correct: a directory will match. You want exists() instead, but this is @Eiyrioü von Kauyf's answer. Furthermore, relative paths are not exactly "relative to the directory where the code is run from". They are instead more generally relative to the "current directory" (which is by default the directory that the code is run from). The current directory can be changed within the program, for instance.

                    – Eric O Lebigot
                    Aug 1 '13 at 5:24















                    The fact that os.path.isfile() matches directories is new to me (and doesn't do as you describe for me on Python 3.3/win), isn't that why there is os.path.isdir() in place to differentiate between the two? In regards to the comment in my post towards the relative path notation neither Oliver Ven Quilnet's nor my example explicitly changes the current directory and I thought I briefly point it out to make it clear for the given context.

                    – bossi
                    Aug 1 '13 at 5:48





                    The fact that os.path.isfile() matches directories is new to me (and doesn't do as you describe for me on Python 3.3/win), isn't that why there is os.path.isdir() in place to differentiate between the two? In regards to the comment in my post towards the relative path notation neither Oliver Ven Quilnet's nor my example explicitly changes the current directory and I thought I briefly point it out to make it clear for the given context.

                    – bossi
                    Aug 1 '13 at 5:48




                    1




                    1





                    You are right, I should have been clearer. I meant that isfile() will make your loop exit when the name matches a directory, and your code tries then to open the directory in write mode, which fails with IOError. This is why isfile() is not the correct test, and should be replaced by the exists() of @Eiyrioü von Kauyf. As for relative paths, I really think that the current "the relative file notation always relates to the file directory/folder you run the code from" is misleading (because of "always").

                    – Eric O Lebigot
                    Aug 1 '13 at 6:41







                    You are right, I should have been clearer. I meant that isfile() will make your loop exit when the name matches a directory, and your code tries then to open the directory in write mode, which fails with IOError. This is why isfile() is not the correct test, and should be replaced by the exists() of @Eiyrioü von Kauyf. As for relative paths, I really think that the current "the relative file notation always relates to the file directory/folder you run the code from" is misleading (because of "always").

                    – Eric O Lebigot
                    Aug 1 '13 at 6:41















                    @EOL: That's a good point, I honestly wasn't aware that identical names between a file and a folder in the same directory are illegal under Windows; thanks for pointing that out. I agree with you, the remark about the relative path did sound misleading, it should sound clearer now.

                    – bossi
                    Aug 1 '13 at 7:52





                    @EOL: That's a good point, I honestly wasn't aware that identical names between a file and a folder in the same directory are illegal under Windows; thanks for pointing that out. I agree with you, the remark about the relative path did sound misleading, it should sound clearer now.

                    – bossi
                    Aug 1 '13 at 7:52













                    10














                    def get_nonexistant_path(fname_path):
                    """
                    Get the path to a filename which does not exist by incrementing path.

                    Examples
                    --------
                    >>> get_nonexistant_path('/etc/issue')
                    '/etc/issue-1'
                    >>> get_nonexistant_path('whatever/1337bla.py')
                    'whatever/1337bla.py'
                    """
                    if not os.path.exists(fname_path):
                    return fname_path
                    filename, file_extension = os.path.splitext(fname_path)
                    i = 1
                    new_fname = "{}-{}{}".format(filename, i, file_extension)
                    while os.path.exists(new_fname):
                    i += 1
                    new_fname = "{}-{}{}".format(filename, i, file_extension)
                    return new_fname


                    Before you open the file, call



                    fname = get_nonexistant_path("sample.xml")


                    This will either give you 'sample.xml' or - if this alreay exists - 'sample-i.xml' where i is the lowest positive integer such that the file does not already exist.



                    I recommend using os.path.abspath("sample.xml"). If you have ~ as home directory, you might need to expand it first.



                    Please note that race conditions might occur with this simple code if you have multiple instances running at the same time. If this might be a problem, please check this question.






                    share|improve this answer






























                      10














                      def get_nonexistant_path(fname_path):
                      """
                      Get the path to a filename which does not exist by incrementing path.

                      Examples
                      --------
                      >>> get_nonexistant_path('/etc/issue')
                      '/etc/issue-1'
                      >>> get_nonexistant_path('whatever/1337bla.py')
                      'whatever/1337bla.py'
                      """
                      if not os.path.exists(fname_path):
                      return fname_path
                      filename, file_extension = os.path.splitext(fname_path)
                      i = 1
                      new_fname = "{}-{}{}".format(filename, i, file_extension)
                      while os.path.exists(new_fname):
                      i += 1
                      new_fname = "{}-{}{}".format(filename, i, file_extension)
                      return new_fname


                      Before you open the file, call



                      fname = get_nonexistant_path("sample.xml")


                      This will either give you 'sample.xml' or - if this alreay exists - 'sample-i.xml' where i is the lowest positive integer such that the file does not already exist.



                      I recommend using os.path.abspath("sample.xml"). If you have ~ as home directory, you might need to expand it first.



                      Please note that race conditions might occur with this simple code if you have multiple instances running at the same time. If this might be a problem, please check this question.






                      share|improve this answer




























                        10












                        10








                        10







                        def get_nonexistant_path(fname_path):
                        """
                        Get the path to a filename which does not exist by incrementing path.

                        Examples
                        --------
                        >>> get_nonexistant_path('/etc/issue')
                        '/etc/issue-1'
                        >>> get_nonexistant_path('whatever/1337bla.py')
                        'whatever/1337bla.py'
                        """
                        if not os.path.exists(fname_path):
                        return fname_path
                        filename, file_extension = os.path.splitext(fname_path)
                        i = 1
                        new_fname = "{}-{}{}".format(filename, i, file_extension)
                        while os.path.exists(new_fname):
                        i += 1
                        new_fname = "{}-{}{}".format(filename, i, file_extension)
                        return new_fname


                        Before you open the file, call



                        fname = get_nonexistant_path("sample.xml")


                        This will either give you 'sample.xml' or - if this alreay exists - 'sample-i.xml' where i is the lowest positive integer such that the file does not already exist.



                        I recommend using os.path.abspath("sample.xml"). If you have ~ as home directory, you might need to expand it first.



                        Please note that race conditions might occur with this simple code if you have multiple instances running at the same time. If this might be a problem, please check this question.






                        share|improve this answer















                        def get_nonexistant_path(fname_path):
                        """
                        Get the path to a filename which does not exist by incrementing path.

                        Examples
                        --------
                        >>> get_nonexistant_path('/etc/issue')
                        '/etc/issue-1'
                        >>> get_nonexistant_path('whatever/1337bla.py')
                        'whatever/1337bla.py'
                        """
                        if not os.path.exists(fname_path):
                        return fname_path
                        filename, file_extension = os.path.splitext(fname_path)
                        i = 1
                        new_fname = "{}-{}{}".format(filename, i, file_extension)
                        while os.path.exists(new_fname):
                        i += 1
                        new_fname = "{}-{}{}".format(filename, i, file_extension)
                        return new_fname


                        Before you open the file, call



                        fname = get_nonexistant_path("sample.xml")


                        This will either give you 'sample.xml' or - if this alreay exists - 'sample-i.xml' where i is the lowest positive integer such that the file does not already exist.



                        I recommend using os.path.abspath("sample.xml"). If you have ~ as home directory, you might need to expand it first.



                        Please note that race conditions might occur with this simple code if you have multiple instances running at the same time. If this might be a problem, please check this question.







                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited May 23 '17 at 12:34









                        Community

                        11




                        11










                        answered Apr 2 '17 at 11:09









                        Martin ThomaMartin Thoma

                        43.4k61307532




                        43.4k61307532























                            4














                            Try setting a count variable, and then incrementing that variable nested inside the same loop you write your file in. Include the count loop inside the name of the file with an escape character, so every loop ticks +1 and so does the number in the file.



                            Some code from a project I just finished:



                            numberLoops = #some limit determined by the user
                            currentLoop = 1
                            while currentLoop < numberLoops:
                            currentLoop = currentLoop + 1

                            fileName = ("log%d_%d.txt" % (currentLoop, str(now())))


                            For reference:



                            from time import mktime, gmtime

                            def now():
                            return mktime(gmtime())


                            which is probably irrelevant in your case but i was running multiple instances of this program and making tons of files. Hope this helps!






                            share|improve this answer





















                            • 2





                              Python has for loops for this, they are much faster to read and comprehend than the while loops that emulate them. Furthermore, the % operator is deprecated. No downvote, though, because it does the job—it just does not do it in the preferred Python way.

                              – Eric O Lebigot
                              Aug 1 '13 at 5:19













                            • There is a problem with your format string: you format a string with %d, and this raises an exception.

                              – Eric O Lebigot
                              Aug 1 '13 at 5:31











                            • Thanks for catching that. It should be a %s, I retyped this rather hastily instead of copying from my source. Thanks!

                              – ford
                              Aug 1 '13 at 5:36
















                            4














                            Try setting a count variable, and then incrementing that variable nested inside the same loop you write your file in. Include the count loop inside the name of the file with an escape character, so every loop ticks +1 and so does the number in the file.



                            Some code from a project I just finished:



                            numberLoops = #some limit determined by the user
                            currentLoop = 1
                            while currentLoop < numberLoops:
                            currentLoop = currentLoop + 1

                            fileName = ("log%d_%d.txt" % (currentLoop, str(now())))


                            For reference:



                            from time import mktime, gmtime

                            def now():
                            return mktime(gmtime())


                            which is probably irrelevant in your case but i was running multiple instances of this program and making tons of files. Hope this helps!






                            share|improve this answer





















                            • 2





                              Python has for loops for this, they are much faster to read and comprehend than the while loops that emulate them. Furthermore, the % operator is deprecated. No downvote, though, because it does the job—it just does not do it in the preferred Python way.

                              – Eric O Lebigot
                              Aug 1 '13 at 5:19













                            • There is a problem with your format string: you format a string with %d, and this raises an exception.

                              – Eric O Lebigot
                              Aug 1 '13 at 5:31











                            • Thanks for catching that. It should be a %s, I retyped this rather hastily instead of copying from my source. Thanks!

                              – ford
                              Aug 1 '13 at 5:36














                            4












                            4








                            4







                            Try setting a count variable, and then incrementing that variable nested inside the same loop you write your file in. Include the count loop inside the name of the file with an escape character, so every loop ticks +1 and so does the number in the file.



                            Some code from a project I just finished:



                            numberLoops = #some limit determined by the user
                            currentLoop = 1
                            while currentLoop < numberLoops:
                            currentLoop = currentLoop + 1

                            fileName = ("log%d_%d.txt" % (currentLoop, str(now())))


                            For reference:



                            from time import mktime, gmtime

                            def now():
                            return mktime(gmtime())


                            which is probably irrelevant in your case but i was running multiple instances of this program and making tons of files. Hope this helps!






                            share|improve this answer















                            Try setting a count variable, and then incrementing that variable nested inside the same loop you write your file in. Include the count loop inside the name of the file with an escape character, so every loop ticks +1 and so does the number in the file.



                            Some code from a project I just finished:



                            numberLoops = #some limit determined by the user
                            currentLoop = 1
                            while currentLoop < numberLoops:
                            currentLoop = currentLoop + 1

                            fileName = ("log%d_%d.txt" % (currentLoop, str(now())))


                            For reference:



                            from time import mktime, gmtime

                            def now():
                            return mktime(gmtime())


                            which is probably irrelevant in your case but i was running multiple instances of this program and making tons of files. Hope this helps!







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Mar 31 '14 at 5:51









                            zanetu

                            2,23311514




                            2,23311514










                            answered Aug 1 '13 at 4:33









                            fordford

                            1301211




                            1301211








                            • 2





                              Python has for loops for this, they are much faster to read and comprehend than the while loops that emulate them. Furthermore, the % operator is deprecated. No downvote, though, because it does the job—it just does not do it in the preferred Python way.

                              – Eric O Lebigot
                              Aug 1 '13 at 5:19













                            • There is a problem with your format string: you format a string with %d, and this raises an exception.

                              – Eric O Lebigot
                              Aug 1 '13 at 5:31











                            • Thanks for catching that. It should be a %s, I retyped this rather hastily instead of copying from my source. Thanks!

                              – ford
                              Aug 1 '13 at 5:36














                            • 2





                              Python has for loops for this, they are much faster to read and comprehend than the while loops that emulate them. Furthermore, the % operator is deprecated. No downvote, though, because it does the job—it just does not do it in the preferred Python way.

                              – Eric O Lebigot
                              Aug 1 '13 at 5:19













                            • There is a problem with your format string: you format a string with %d, and this raises an exception.

                              – Eric O Lebigot
                              Aug 1 '13 at 5:31











                            • Thanks for catching that. It should be a %s, I retyped this rather hastily instead of copying from my source. Thanks!

                              – ford
                              Aug 1 '13 at 5:36








                            2




                            2





                            Python has for loops for this, they are much faster to read and comprehend than the while loops that emulate them. Furthermore, the % operator is deprecated. No downvote, though, because it does the job—it just does not do it in the preferred Python way.

                            – Eric O Lebigot
                            Aug 1 '13 at 5:19







                            Python has for loops for this, they are much faster to read and comprehend than the while loops that emulate them. Furthermore, the % operator is deprecated. No downvote, though, because it does the job—it just does not do it in the preferred Python way.

                            – Eric O Lebigot
                            Aug 1 '13 at 5:19















                            There is a problem with your format string: you format a string with %d, and this raises an exception.

                            – Eric O Lebigot
                            Aug 1 '13 at 5:31





                            There is a problem with your format string: you format a string with %d, and this raises an exception.

                            – Eric O Lebigot
                            Aug 1 '13 at 5:31













                            Thanks for catching that. It should be a %s, I retyped this rather hastily instead of copying from my source. Thanks!

                            – ford
                            Aug 1 '13 at 5:36





                            Thanks for catching that. It should be a %s, I retyped this rather hastily instead of copying from my source. Thanks!

                            – ford
                            Aug 1 '13 at 5:36











                            4














                            Sequentially checking each file name to find the next available one works fine with small numbers of files, but quickly becomes slower as the number of files increases.



                            Here is a version that finds the next available file name in log(n) time:



                            import os

                            def next_path(path_pattern):
                            """
                            Finds the next free path in an sequentially named list of files

                            e.g. path_pattern = 'file-%s.txt':

                            file-1.txt
                            file-2.txt
                            file-3.txt

                            Runs in log(n) time where n is the number of existing files in sequence
                            """
                            i = 1

                            # First do an exponential search
                            while os.path.exists(path_pattern % i):
                            i = i * 2

                            # Result lies somewhere in the interval (i/2..i]
                            # We call this interval (a..b] and narrow it down until a + 1 = b
                            a, b = (i / 2, i)
                            while a + 1 < b:
                            c = (a + b) / 2 # interval midpoint
                            a, b = (c, b) if os.path.exists(path_pattern % c) else (a, c)

                            return path_pattern % b


                            To measure the speed improvement I wrote a small test function that creates 10,000 files:



                            for i in range(1,10000):
                            with open(next_path('file-%s.foo'), 'w'):
                            pass


                            And implemented the naive approach:



                            def next_path_naive(path_pattern):
                            """
                            Naive (slow) version of next_path
                            """
                            i = 1
                            while os.path.exists(path_pattern % i):
                            i += 1
                            return path_pattern % i


                            And here are the results:



                            Fast version:



                            real    0m2.132s
                            user 0m0.773s
                            sys 0m1.312s


                            Naive version:



                            real    2m36.480s
                            user 1m12.671s
                            sys 1m22.425s


                            Finally, note that either approach is susceptible to race conditions if multiple actors are trying to create files in the sequence at the same time.






                            share|improve this answer




























                              4














                              Sequentially checking each file name to find the next available one works fine with small numbers of files, but quickly becomes slower as the number of files increases.



                              Here is a version that finds the next available file name in log(n) time:



                              import os

                              def next_path(path_pattern):
                              """
                              Finds the next free path in an sequentially named list of files

                              e.g. path_pattern = 'file-%s.txt':

                              file-1.txt
                              file-2.txt
                              file-3.txt

                              Runs in log(n) time where n is the number of existing files in sequence
                              """
                              i = 1

                              # First do an exponential search
                              while os.path.exists(path_pattern % i):
                              i = i * 2

                              # Result lies somewhere in the interval (i/2..i]
                              # We call this interval (a..b] and narrow it down until a + 1 = b
                              a, b = (i / 2, i)
                              while a + 1 < b:
                              c = (a + b) / 2 # interval midpoint
                              a, b = (c, b) if os.path.exists(path_pattern % c) else (a, c)

                              return path_pattern % b


                              To measure the speed improvement I wrote a small test function that creates 10,000 files:



                              for i in range(1,10000):
                              with open(next_path('file-%s.foo'), 'w'):
                              pass


                              And implemented the naive approach:



                              def next_path_naive(path_pattern):
                              """
                              Naive (slow) version of next_path
                              """
                              i = 1
                              while os.path.exists(path_pattern % i):
                              i += 1
                              return path_pattern % i


                              And here are the results:



                              Fast version:



                              real    0m2.132s
                              user 0m0.773s
                              sys 0m1.312s


                              Naive version:



                              real    2m36.480s
                              user 1m12.671s
                              sys 1m22.425s


                              Finally, note that either approach is susceptible to race conditions if multiple actors are trying to create files in the sequence at the same time.






                              share|improve this answer


























                                4












                                4








                                4







                                Sequentially checking each file name to find the next available one works fine with small numbers of files, but quickly becomes slower as the number of files increases.



                                Here is a version that finds the next available file name in log(n) time:



                                import os

                                def next_path(path_pattern):
                                """
                                Finds the next free path in an sequentially named list of files

                                e.g. path_pattern = 'file-%s.txt':

                                file-1.txt
                                file-2.txt
                                file-3.txt

                                Runs in log(n) time where n is the number of existing files in sequence
                                """
                                i = 1

                                # First do an exponential search
                                while os.path.exists(path_pattern % i):
                                i = i * 2

                                # Result lies somewhere in the interval (i/2..i]
                                # We call this interval (a..b] and narrow it down until a + 1 = b
                                a, b = (i / 2, i)
                                while a + 1 < b:
                                c = (a + b) / 2 # interval midpoint
                                a, b = (c, b) if os.path.exists(path_pattern % c) else (a, c)

                                return path_pattern % b


                                To measure the speed improvement I wrote a small test function that creates 10,000 files:



                                for i in range(1,10000):
                                with open(next_path('file-%s.foo'), 'w'):
                                pass


                                And implemented the naive approach:



                                def next_path_naive(path_pattern):
                                """
                                Naive (slow) version of next_path
                                """
                                i = 1
                                while os.path.exists(path_pattern % i):
                                i += 1
                                return path_pattern % i


                                And here are the results:



                                Fast version:



                                real    0m2.132s
                                user 0m0.773s
                                sys 0m1.312s


                                Naive version:



                                real    2m36.480s
                                user 1m12.671s
                                sys 1m22.425s


                                Finally, note that either approach is susceptible to race conditions if multiple actors are trying to create files in the sequence at the same time.






                                share|improve this answer













                                Sequentially checking each file name to find the next available one works fine with small numbers of files, but quickly becomes slower as the number of files increases.



                                Here is a version that finds the next available file name in log(n) time:



                                import os

                                def next_path(path_pattern):
                                """
                                Finds the next free path in an sequentially named list of files

                                e.g. path_pattern = 'file-%s.txt':

                                file-1.txt
                                file-2.txt
                                file-3.txt

                                Runs in log(n) time where n is the number of existing files in sequence
                                """
                                i = 1

                                # First do an exponential search
                                while os.path.exists(path_pattern % i):
                                i = i * 2

                                # Result lies somewhere in the interval (i/2..i]
                                # We call this interval (a..b] and narrow it down until a + 1 = b
                                a, b = (i / 2, i)
                                while a + 1 < b:
                                c = (a + b) / 2 # interval midpoint
                                a, b = (c, b) if os.path.exists(path_pattern % c) else (a, c)

                                return path_pattern % b


                                To measure the speed improvement I wrote a small test function that creates 10,000 files:



                                for i in range(1,10000):
                                with open(next_path('file-%s.foo'), 'w'):
                                pass


                                And implemented the naive approach:



                                def next_path_naive(path_pattern):
                                """
                                Naive (slow) version of next_path
                                """
                                i = 1
                                while os.path.exists(path_pattern % i):
                                i += 1
                                return path_pattern % i


                                And here are the results:



                                Fast version:



                                real    0m2.132s
                                user 0m0.773s
                                sys 0m1.312s


                                Naive version:



                                real    2m36.480s
                                user 1m12.671s
                                sys 1m22.425s


                                Finally, note that either approach is susceptible to race conditions if multiple actors are trying to create files in the sequence at the same time.







                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Nov 3 '17 at 2:20









                                JamesJames

                                2,0772128




                                2,0772128























                                    1














                                    Without storing state data in an extra file, a quicker solution to the ones presented here would be to do the following:



                                    from glob import glob
                                    import os

                                    files = glob("somedir/sample*.xml")
                                    files = files.sorted()
                                    cur_num = int(os.path.basename(files[-1])[6:-4])
                                    cur_num += 1
                                    fh = open("somedir/sample%s.xml" % cur_num, 'w')
                                    rs = [blockresult]
                                    fh.writelines(rs)
                                    fh.close()


                                    This will also keep incrementing, even if some of the lower numbered files disappear.



                                    The other solution here that I like (pointed out by Eiyrioü) is the idea of keeping a temporary file that contains your most recent number:



                                    temp_fh = open('somedir/curr_num.txt', 'r')
                                    curr_num = int(temp_fh.readline().strip())
                                    curr_num += 1
                                    fh = open("somedir/sample%s.xml" % cur_num, 'w')
                                    rs = [blockresult]
                                    fh.writelines(rs)
                                    fh.close()





                                    share|improve this answer


























                                    • Your cur_num calculation only works for 1-digit numbers, it is not general enough.

                                      – Eric O Lebigot
                                      Aug 1 '13 at 5:21











                                    • Good point, updated.

                                      – Vorticity
                                      Aug 1 '13 at 5:25


















                                    1














                                    Without storing state data in an extra file, a quicker solution to the ones presented here would be to do the following:



                                    from glob import glob
                                    import os

                                    files = glob("somedir/sample*.xml")
                                    files = files.sorted()
                                    cur_num = int(os.path.basename(files[-1])[6:-4])
                                    cur_num += 1
                                    fh = open("somedir/sample%s.xml" % cur_num, 'w')
                                    rs = [blockresult]
                                    fh.writelines(rs)
                                    fh.close()


                                    This will also keep incrementing, even if some of the lower numbered files disappear.



                                    The other solution here that I like (pointed out by Eiyrioü) is the idea of keeping a temporary file that contains your most recent number:



                                    temp_fh = open('somedir/curr_num.txt', 'r')
                                    curr_num = int(temp_fh.readline().strip())
                                    curr_num += 1
                                    fh = open("somedir/sample%s.xml" % cur_num, 'w')
                                    rs = [blockresult]
                                    fh.writelines(rs)
                                    fh.close()





                                    share|improve this answer


























                                    • Your cur_num calculation only works for 1-digit numbers, it is not general enough.

                                      – Eric O Lebigot
                                      Aug 1 '13 at 5:21











                                    • Good point, updated.

                                      – Vorticity
                                      Aug 1 '13 at 5:25
















                                    1












                                    1








                                    1







                                    Without storing state data in an extra file, a quicker solution to the ones presented here would be to do the following:



                                    from glob import glob
                                    import os

                                    files = glob("somedir/sample*.xml")
                                    files = files.sorted()
                                    cur_num = int(os.path.basename(files[-1])[6:-4])
                                    cur_num += 1
                                    fh = open("somedir/sample%s.xml" % cur_num, 'w')
                                    rs = [blockresult]
                                    fh.writelines(rs)
                                    fh.close()


                                    This will also keep incrementing, even if some of the lower numbered files disappear.



                                    The other solution here that I like (pointed out by Eiyrioü) is the idea of keeping a temporary file that contains your most recent number:



                                    temp_fh = open('somedir/curr_num.txt', 'r')
                                    curr_num = int(temp_fh.readline().strip())
                                    curr_num += 1
                                    fh = open("somedir/sample%s.xml" % cur_num, 'w')
                                    rs = [blockresult]
                                    fh.writelines(rs)
                                    fh.close()





                                    share|improve this answer















                                    Without storing state data in an extra file, a quicker solution to the ones presented here would be to do the following:



                                    from glob import glob
                                    import os

                                    files = glob("somedir/sample*.xml")
                                    files = files.sorted()
                                    cur_num = int(os.path.basename(files[-1])[6:-4])
                                    cur_num += 1
                                    fh = open("somedir/sample%s.xml" % cur_num, 'w')
                                    rs = [blockresult]
                                    fh.writelines(rs)
                                    fh.close()


                                    This will also keep incrementing, even if some of the lower numbered files disappear.



                                    The other solution here that I like (pointed out by Eiyrioü) is the idea of keeping a temporary file that contains your most recent number:



                                    temp_fh = open('somedir/curr_num.txt', 'r')
                                    curr_num = int(temp_fh.readline().strip())
                                    curr_num += 1
                                    fh = open("somedir/sample%s.xml" % cur_num, 'w')
                                    rs = [blockresult]
                                    fh.writelines(rs)
                                    fh.close()






                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Aug 1 '13 at 5:24

























                                    answered Aug 1 '13 at 5:06









                                    VorticityVorticity

                                    2,30321837




                                    2,30321837













                                    • Your cur_num calculation only works for 1-digit numbers, it is not general enough.

                                      – Eric O Lebigot
                                      Aug 1 '13 at 5:21











                                    • Good point, updated.

                                      – Vorticity
                                      Aug 1 '13 at 5:25





















                                    • Your cur_num calculation only works for 1-digit numbers, it is not general enough.

                                      – Eric O Lebigot
                                      Aug 1 '13 at 5:21











                                    • Good point, updated.

                                      – Vorticity
                                      Aug 1 '13 at 5:25



















                                    Your cur_num calculation only works for 1-digit numbers, it is not general enough.

                                    – Eric O Lebigot
                                    Aug 1 '13 at 5:21





                                    Your cur_num calculation only works for 1-digit numbers, it is not general enough.

                                    – Eric O Lebigot
                                    Aug 1 '13 at 5:21













                                    Good point, updated.

                                    – Vorticity
                                    Aug 1 '13 at 5:25







                                    Good point, updated.

                                    – Vorticity
                                    Aug 1 '13 at 5:25













                                    1














                                    Another example using recursion



                                    import os
                                    def checkFilePath(testString, extension, currentCount):
                                    if os.path.exists(testString + str(currentCount) +extension):
                                    return checkFilePath(testString, extension, currentCount+1)
                                    else:
                                    return testString + str(currentCount) +extension


                                    Use:



                                    checkFilePath("myfile", ".txt" , 0)





                                    share|improve this answer




























                                      1














                                      Another example using recursion



                                      import os
                                      def checkFilePath(testString, extension, currentCount):
                                      if os.path.exists(testString + str(currentCount) +extension):
                                      return checkFilePath(testString, extension, currentCount+1)
                                      else:
                                      return testString + str(currentCount) +extension


                                      Use:



                                      checkFilePath("myfile", ".txt" , 0)





                                      share|improve this answer


























                                        1












                                        1








                                        1







                                        Another example using recursion



                                        import os
                                        def checkFilePath(testString, extension, currentCount):
                                        if os.path.exists(testString + str(currentCount) +extension):
                                        return checkFilePath(testString, extension, currentCount+1)
                                        else:
                                        return testString + str(currentCount) +extension


                                        Use:



                                        checkFilePath("myfile", ".txt" , 0)





                                        share|improve this answer













                                        Another example using recursion



                                        import os
                                        def checkFilePath(testString, extension, currentCount):
                                        if os.path.exists(testString + str(currentCount) +extension):
                                        return checkFilePath(testString, extension, currentCount+1)
                                        else:
                                        return testString + str(currentCount) +extension


                                        Use:



                                        checkFilePath("myfile", ".txt" , 0)






                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Feb 15 '17 at 3:04









                                        chumbaloochumbaloo

                                        1409




                                        1409























                                            1














                                            You can use a while loop with a counter which checks if a file with a name and the counter's value exists if it does then move on else break and make a file.



                                            I have done it in this way for one of my projects:`



                                            from os import path
                                            import os

                                            i = 0
                                            flnm = "Directory\Filename" + str(i) + ".txt"
                                            while path.exists(flnm) :
                                            flnm = "Directory\Filename" + str(i) + ".txt"
                                            i += 1
                                            f = open(flnm, "w") #do what you want to with that file...
                                            f.write(str(var))
                                            f.close() # make sure to close it.


                                            `



                                            Here the counter i starts from 0 and a while loop checks everytime if the file exists, if it does it moves on else it breaks out and creates a file from then you can customize. Also make sure to close it else it will result in the file being open which can cause problems while deleting it.
                                            I used path.exists() to check if a file exists.
                                            Don't do from os import * it can cause problem when we use open() method as there is another os.open() method too and it can give the error. TypeError: Integer expected. (got str)
                                            Else wish u a Happy New Year and to all.






                                            share|improve this answer
























                                            • Thanks For Rating!

                                              – Matrix Programmer
                                              Jan 31 at 10:41
















                                            1














                                            You can use a while loop with a counter which checks if a file with a name and the counter's value exists if it does then move on else break and make a file.



                                            I have done it in this way for one of my projects:`



                                            from os import path
                                            import os

                                            i = 0
                                            flnm = "Directory\Filename" + str(i) + ".txt"
                                            while path.exists(flnm) :
                                            flnm = "Directory\Filename" + str(i) + ".txt"
                                            i += 1
                                            f = open(flnm, "w") #do what you want to with that file...
                                            f.write(str(var))
                                            f.close() # make sure to close it.


                                            `



                                            Here the counter i starts from 0 and a while loop checks everytime if the file exists, if it does it moves on else it breaks out and creates a file from then you can customize. Also make sure to close it else it will result in the file being open which can cause problems while deleting it.
                                            I used path.exists() to check if a file exists.
                                            Don't do from os import * it can cause problem when we use open() method as there is another os.open() method too and it can give the error. TypeError: Integer expected. (got str)
                                            Else wish u a Happy New Year and to all.






                                            share|improve this answer
























                                            • Thanks For Rating!

                                              – Matrix Programmer
                                              Jan 31 at 10:41














                                            1












                                            1








                                            1







                                            You can use a while loop with a counter which checks if a file with a name and the counter's value exists if it does then move on else break and make a file.



                                            I have done it in this way for one of my projects:`



                                            from os import path
                                            import os

                                            i = 0
                                            flnm = "Directory\Filename" + str(i) + ".txt"
                                            while path.exists(flnm) :
                                            flnm = "Directory\Filename" + str(i) + ".txt"
                                            i += 1
                                            f = open(flnm, "w") #do what you want to with that file...
                                            f.write(str(var))
                                            f.close() # make sure to close it.


                                            `



                                            Here the counter i starts from 0 and a while loop checks everytime if the file exists, if it does it moves on else it breaks out and creates a file from then you can customize. Also make sure to close it else it will result in the file being open which can cause problems while deleting it.
                                            I used path.exists() to check if a file exists.
                                            Don't do from os import * it can cause problem when we use open() method as there is another os.open() method too and it can give the error. TypeError: Integer expected. (got str)
                                            Else wish u a Happy New Year and to all.






                                            share|improve this answer













                                            You can use a while loop with a counter which checks if a file with a name and the counter's value exists if it does then move on else break and make a file.



                                            I have done it in this way for one of my projects:`



                                            from os import path
                                            import os

                                            i = 0
                                            flnm = "Directory\Filename" + str(i) + ".txt"
                                            while path.exists(flnm) :
                                            flnm = "Directory\Filename" + str(i) + ".txt"
                                            i += 1
                                            f = open(flnm, "w") #do what you want to with that file...
                                            f.write(str(var))
                                            f.close() # make sure to close it.


                                            `



                                            Here the counter i starts from 0 and a while loop checks everytime if the file exists, if it does it moves on else it breaks out and creates a file from then you can customize. Also make sure to close it else it will result in the file being open which can cause problems while deleting it.
                                            I used path.exists() to check if a file exists.
                                            Don't do from os import * it can cause problem when we use open() method as there is another os.open() method too and it can give the error. TypeError: Integer expected. (got str)
                                            Else wish u a Happy New Year and to all.







                                            share|improve this answer












                                            share|improve this answer



                                            share|improve this answer










                                            answered Jan 1 at 7:26









                                            Matrix ProgrammerMatrix Programmer

                                            1112




                                            1112













                                            • Thanks For Rating!

                                              – Matrix Programmer
                                              Jan 31 at 10:41



















                                            • Thanks For Rating!

                                              – Matrix Programmer
                                              Jan 31 at 10:41

















                                            Thanks For Rating!

                                            – Matrix Programmer
                                            Jan 31 at 10:41





                                            Thanks For Rating!

                                            – Matrix Programmer
                                            Jan 31 at 10:41











                                            0














                                            The two ways to do it are:




                                            1. Check for the existence of the old file and if it exists try the next file name +1

                                            2. save state data somewhere




                                            an easy way to do it off the bat would be:



                                            import os.path as pth
                                            filename = "myfile"
                                            filenum = 1
                                            while (pth.exists(pth.abspath(filename+str(filenum)+".py")):
                                            filenum+=1
                                            my_next_file = open(filename+str(filenum)+".py",'w')


                                            as a design thing, while True slows things down and isn't a great thing for code readability





                                            edited: @EOL contributions/ thoughts



                                            so I think not having .format is more readable at first glance - but using .format is better for generality and convention so.



                                            import os.path as pth
                                            filename = "myfile"
                                            filenum = 1
                                            while (pth.exists(pth.abspath(filename+str(filenum)+".py")):
                                            filenum+=1
                                            my_next_file = open("{}{}.py".format(filename, filenum),'w')
                                            # or
                                            my_next_file = open(filename + "{}.py".format(filenum),'w')


                                            and you don't have to use abspath - you can use relative paths if you prefer, I prefer abs path sometimes because it helps to normalize the paths passed :).



                                            import os.path as pth
                                            filename = "myfile"
                                            filenum = 1
                                            while (pth.exists(filename+str(filenum)+".py"):
                                            filenum+=1
                                            ##removed for conciseness





                                            share|improve this answer


























                                            • The format() method is much more legible than string concatenation, here. I think that the while loop is fine, here. On another topic, why use abspath()?

                                              – Eric O Lebigot
                                              Aug 1 '13 at 5:23











                                            • format is more legible, but then he would have to look at string formatting; this is easier to understand on first glance imho. and abspath because i'm ignoring symlinks :/ .... that could lead to confusing errors

                                              – Eiyrioü von Kauyf
                                              Aug 1 '13 at 5:26













                                            • While I understand your point, I believe that even beginners should be shown Pythonic examples, so that they take good habits. The behavior of format() is really quite simple to understand and even guess: "{}{}.py".format(filename, filenum). It's even simpler than the algorithm presented here. :)

                                              – Eric O Lebigot
                                              Aug 1 '13 at 5:29











                                            • hmm added :) i agree with you on this point.

                                              – Eiyrioü von Kauyf
                                              Aug 1 '13 at 5:31











                                            • @EOL whatcha think ;) do I have your approval

                                              – Eiyrioü von Kauyf
                                              Aug 1 '13 at 5:34


















                                            0














                                            The two ways to do it are:




                                            1. Check for the existence of the old file and if it exists try the next file name +1

                                            2. save state data somewhere




                                            an easy way to do it off the bat would be:



                                            import os.path as pth
                                            filename = "myfile"
                                            filenum = 1
                                            while (pth.exists(pth.abspath(filename+str(filenum)+".py")):
                                            filenum+=1
                                            my_next_file = open(filename+str(filenum)+".py",'w')


                                            as a design thing, while True slows things down and isn't a great thing for code readability





                                            edited: @EOL contributions/ thoughts



                                            so I think not having .format is more readable at first glance - but using .format is better for generality and convention so.



                                            import os.path as pth
                                            filename = "myfile"
                                            filenum = 1
                                            while (pth.exists(pth.abspath(filename+str(filenum)+".py")):
                                            filenum+=1
                                            my_next_file = open("{}{}.py".format(filename, filenum),'w')
                                            # or
                                            my_next_file = open(filename + "{}.py".format(filenum),'w')


                                            and you don't have to use abspath - you can use relative paths if you prefer, I prefer abs path sometimes because it helps to normalize the paths passed :).



                                            import os.path as pth
                                            filename = "myfile"
                                            filenum = 1
                                            while (pth.exists(filename+str(filenum)+".py"):
                                            filenum+=1
                                            ##removed for conciseness





                                            share|improve this answer


























                                            • The format() method is much more legible than string concatenation, here. I think that the while loop is fine, here. On another topic, why use abspath()?

                                              – Eric O Lebigot
                                              Aug 1 '13 at 5:23











                                            • format is more legible, but then he would have to look at string formatting; this is easier to understand on first glance imho. and abspath because i'm ignoring symlinks :/ .... that could lead to confusing errors

                                              – Eiyrioü von Kauyf
                                              Aug 1 '13 at 5:26













                                            • While I understand your point, I believe that even beginners should be shown Pythonic examples, so that they take good habits. The behavior of format() is really quite simple to understand and even guess: "{}{}.py".format(filename, filenum). It's even simpler than the algorithm presented here. :)

                                              – Eric O Lebigot
                                              Aug 1 '13 at 5:29











                                            • hmm added :) i agree with you on this point.

                                              – Eiyrioü von Kauyf
                                              Aug 1 '13 at 5:31











                                            • @EOL whatcha think ;) do I have your approval

                                              – Eiyrioü von Kauyf
                                              Aug 1 '13 at 5:34
















                                            0












                                            0








                                            0







                                            The two ways to do it are:




                                            1. Check for the existence of the old file and if it exists try the next file name +1

                                            2. save state data somewhere




                                            an easy way to do it off the bat would be:



                                            import os.path as pth
                                            filename = "myfile"
                                            filenum = 1
                                            while (pth.exists(pth.abspath(filename+str(filenum)+".py")):
                                            filenum+=1
                                            my_next_file = open(filename+str(filenum)+".py",'w')


                                            as a design thing, while True slows things down and isn't a great thing for code readability





                                            edited: @EOL contributions/ thoughts



                                            so I think not having .format is more readable at first glance - but using .format is better for generality and convention so.



                                            import os.path as pth
                                            filename = "myfile"
                                            filenum = 1
                                            while (pth.exists(pth.abspath(filename+str(filenum)+".py")):
                                            filenum+=1
                                            my_next_file = open("{}{}.py".format(filename, filenum),'w')
                                            # or
                                            my_next_file = open(filename + "{}.py".format(filenum),'w')


                                            and you don't have to use abspath - you can use relative paths if you prefer, I prefer abs path sometimes because it helps to normalize the paths passed :).



                                            import os.path as pth
                                            filename = "myfile"
                                            filenum = 1
                                            while (pth.exists(filename+str(filenum)+".py"):
                                            filenum+=1
                                            ##removed for conciseness





                                            share|improve this answer















                                            The two ways to do it are:




                                            1. Check for the existence of the old file and if it exists try the next file name +1

                                            2. save state data somewhere




                                            an easy way to do it off the bat would be:



                                            import os.path as pth
                                            filename = "myfile"
                                            filenum = 1
                                            while (pth.exists(pth.abspath(filename+str(filenum)+".py")):
                                            filenum+=1
                                            my_next_file = open(filename+str(filenum)+".py",'w')


                                            as a design thing, while True slows things down and isn't a great thing for code readability





                                            edited: @EOL contributions/ thoughts



                                            so I think not having .format is more readable at first glance - but using .format is better for generality and convention so.



                                            import os.path as pth
                                            filename = "myfile"
                                            filenum = 1
                                            while (pth.exists(pth.abspath(filename+str(filenum)+".py")):
                                            filenum+=1
                                            my_next_file = open("{}{}.py".format(filename, filenum),'w')
                                            # or
                                            my_next_file = open(filename + "{}.py".format(filenum),'w')


                                            and you don't have to use abspath - you can use relative paths if you prefer, I prefer abs path sometimes because it helps to normalize the paths passed :).



                                            import os.path as pth
                                            filename = "myfile"
                                            filenum = 1
                                            while (pth.exists(filename+str(filenum)+".py"):
                                            filenum+=1
                                            ##removed for conciseness






                                            share|improve this answer














                                            share|improve this answer



                                            share|improve this answer








                                            edited Aug 1 '13 at 5:33

























                                            answered Aug 1 '13 at 3:33









                                            Eiyrioü von KauyfEiyrioü von Kauyf

                                            1,72132336




                                            1,72132336













                                            • The format() method is much more legible than string concatenation, here. I think that the while loop is fine, here. On another topic, why use abspath()?

                                              – Eric O Lebigot
                                              Aug 1 '13 at 5:23











                                            • format is more legible, but then he would have to look at string formatting; this is easier to understand on first glance imho. and abspath because i'm ignoring symlinks :/ .... that could lead to confusing errors

                                              – Eiyrioü von Kauyf
                                              Aug 1 '13 at 5:26













                                            • While I understand your point, I believe that even beginners should be shown Pythonic examples, so that they take good habits. The behavior of format() is really quite simple to understand and even guess: "{}{}.py".format(filename, filenum). It's even simpler than the algorithm presented here. :)

                                              – Eric O Lebigot
                                              Aug 1 '13 at 5:29











                                            • hmm added :) i agree with you on this point.

                                              – Eiyrioü von Kauyf
                                              Aug 1 '13 at 5:31











                                            • @EOL whatcha think ;) do I have your approval

                                              – Eiyrioü von Kauyf
                                              Aug 1 '13 at 5:34





















                                            • The format() method is much more legible than string concatenation, here. I think that the while loop is fine, here. On another topic, why use abspath()?

                                              – Eric O Lebigot
                                              Aug 1 '13 at 5:23











                                            • format is more legible, but then he would have to look at string formatting; this is easier to understand on first glance imho. and abspath because i'm ignoring symlinks :/ .... that could lead to confusing errors

                                              – Eiyrioü von Kauyf
                                              Aug 1 '13 at 5:26













                                            • While I understand your point, I believe that even beginners should be shown Pythonic examples, so that they take good habits. The behavior of format() is really quite simple to understand and even guess: "{}{}.py".format(filename, filenum). It's even simpler than the algorithm presented here. :)

                                              – Eric O Lebigot
                                              Aug 1 '13 at 5:29











                                            • hmm added :) i agree with you on this point.

                                              – Eiyrioü von Kauyf
                                              Aug 1 '13 at 5:31











                                            • @EOL whatcha think ;) do I have your approval

                                              – Eiyrioü von Kauyf
                                              Aug 1 '13 at 5:34



















                                            The format() method is much more legible than string concatenation, here. I think that the while loop is fine, here. On another topic, why use abspath()?

                                            – Eric O Lebigot
                                            Aug 1 '13 at 5:23





                                            The format() method is much more legible than string concatenation, here. I think that the while loop is fine, here. On another topic, why use abspath()?

                                            – Eric O Lebigot
                                            Aug 1 '13 at 5:23













                                            format is more legible, but then he would have to look at string formatting; this is easier to understand on first glance imho. and abspath because i'm ignoring symlinks :/ .... that could lead to confusing errors

                                            – Eiyrioü von Kauyf
                                            Aug 1 '13 at 5:26







                                            format is more legible, but then he would have to look at string formatting; this is easier to understand on first glance imho. and abspath because i'm ignoring symlinks :/ .... that could lead to confusing errors

                                            – Eiyrioü von Kauyf
                                            Aug 1 '13 at 5:26















                                            While I understand your point, I believe that even beginners should be shown Pythonic examples, so that they take good habits. The behavior of format() is really quite simple to understand and even guess: "{}{}.py".format(filename, filenum). It's even simpler than the algorithm presented here. :)

                                            – Eric O Lebigot
                                            Aug 1 '13 at 5:29





                                            While I understand your point, I believe that even beginners should be shown Pythonic examples, so that they take good habits. The behavior of format() is really quite simple to understand and even guess: "{}{}.py".format(filename, filenum). It's even simpler than the algorithm presented here. :)

                                            – Eric O Lebigot
                                            Aug 1 '13 at 5:29













                                            hmm added :) i agree with you on this point.

                                            – Eiyrioü von Kauyf
                                            Aug 1 '13 at 5:31





                                            hmm added :) i agree with you on this point.

                                            – Eiyrioü von Kauyf
                                            Aug 1 '13 at 5:31













                                            @EOL whatcha think ;) do I have your approval

                                            – Eiyrioü von Kauyf
                                            Aug 1 '13 at 5:34







                                            @EOL whatcha think ;) do I have your approval

                                            – Eiyrioü von Kauyf
                                            Aug 1 '13 at 5:34













                                            0














                                            Another solution that avoids the use of while loop is to use os.listdir() function which returns a list of all the files and directories contained in a directory whose path is taken as an argument.



                                            To answer the example in the question, supposing that the directory you are working in only contains "sample_i.xlm" files indexed starting at 0, you can easily obtain the next index for the new file with the following code.



                                            import os

                                            new_index = len(os.listdir('path_to_file_containing_only_sample_i_files'))
                                            new_file = open('path_to_file_containing_only_sample_i_files/sample_%s.xml' % new_index, 'w')





                                            share|improve this answer




























                                              0














                                              Another solution that avoids the use of while loop is to use os.listdir() function which returns a list of all the files and directories contained in a directory whose path is taken as an argument.



                                              To answer the example in the question, supposing that the directory you are working in only contains "sample_i.xlm" files indexed starting at 0, you can easily obtain the next index for the new file with the following code.



                                              import os

                                              new_index = len(os.listdir('path_to_file_containing_only_sample_i_files'))
                                              new_file = open('path_to_file_containing_only_sample_i_files/sample_%s.xml' % new_index, 'w')





                                              share|improve this answer


























                                                0












                                                0








                                                0







                                                Another solution that avoids the use of while loop is to use os.listdir() function which returns a list of all the files and directories contained in a directory whose path is taken as an argument.



                                                To answer the example in the question, supposing that the directory you are working in only contains "sample_i.xlm" files indexed starting at 0, you can easily obtain the next index for the new file with the following code.



                                                import os

                                                new_index = len(os.listdir('path_to_file_containing_only_sample_i_files'))
                                                new_file = open('path_to_file_containing_only_sample_i_files/sample_%s.xml' % new_index, 'w')





                                                share|improve this answer













                                                Another solution that avoids the use of while loop is to use os.listdir() function which returns a list of all the files and directories contained in a directory whose path is taken as an argument.



                                                To answer the example in the question, supposing that the directory you are working in only contains "sample_i.xlm" files indexed starting at 0, you can easily obtain the next index for the new file with the following code.



                                                import os

                                                new_index = len(os.listdir('path_to_file_containing_only_sample_i_files'))
                                                new_file = open('path_to_file_containing_only_sample_i_files/sample_%s.xml' % new_index, 'w')






                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered Aug 30 '18 at 9:43









                                                Malo PocheauMalo Pocheau

                                                264




                                                264























                                                    0














                                                    I needed to do something similar, but for output directories in a data processing pipeline. I was inspired by Vorticity's answer, but added use of regex to grab the trailing number. This method continues to increment the last directory, even if intermediate numbered output directories are deleted. It also adds leading zeros so the names will sort alphabetically (i.e. width 3 gives 001 etc.)



                                                    def get_unique_dir(path, width=3):
                                                    # if it doesn't exist, create
                                                    if not os.path.isdir(path):
                                                    log.debug("Creating new directory - {}".format(path))
                                                    os.makedirs(path)
                                                    return path

                                                    # if it's empty, use
                                                    if not os.listdir(path):
                                                    log.debug("Using empty directory - {}".format(path))
                                                    return path

                                                    # otherwise, increment the highest number folder in the series

                                                    def get_trailing_number(search_text):
                                                    serch_obj = re.search(r"([0-9]+)$", search_text)
                                                    if not serch_obj:
                                                    return 0
                                                    else:
                                                    return int(serch_obj.group(1))

                                                    dirs = glob(path + "*")
                                                    num_list = sorted([get_trailing_number(d) for d in dirs])
                                                    highest_num = num_list[-1]
                                                    next_num = highest_num + 1
                                                    new_path = "{0}_{1:0>{2}}".format(path, next_num, width)

                                                    log.debug("Creating new incremented directory - {}".format(new_path))
                                                    os.makedirs(new_path)
                                                    return new_path

                                                    get_unique_dir("output")





                                                    share|improve this answer






























                                                      0














                                                      I needed to do something similar, but for output directories in a data processing pipeline. I was inspired by Vorticity's answer, but added use of regex to grab the trailing number. This method continues to increment the last directory, even if intermediate numbered output directories are deleted. It also adds leading zeros so the names will sort alphabetically (i.e. width 3 gives 001 etc.)



                                                      def get_unique_dir(path, width=3):
                                                      # if it doesn't exist, create
                                                      if not os.path.isdir(path):
                                                      log.debug("Creating new directory - {}".format(path))
                                                      os.makedirs(path)
                                                      return path

                                                      # if it's empty, use
                                                      if not os.listdir(path):
                                                      log.debug("Using empty directory - {}".format(path))
                                                      return path

                                                      # otherwise, increment the highest number folder in the series

                                                      def get_trailing_number(search_text):
                                                      serch_obj = re.search(r"([0-9]+)$", search_text)
                                                      if not serch_obj:
                                                      return 0
                                                      else:
                                                      return int(serch_obj.group(1))

                                                      dirs = glob(path + "*")
                                                      num_list = sorted([get_trailing_number(d) for d in dirs])
                                                      highest_num = num_list[-1]
                                                      next_num = highest_num + 1
                                                      new_path = "{0}_{1:0>{2}}".format(path, next_num, width)

                                                      log.debug("Creating new incremented directory - {}".format(new_path))
                                                      os.makedirs(new_path)
                                                      return new_path

                                                      get_unique_dir("output")





                                                      share|improve this answer




























                                                        0












                                                        0








                                                        0







                                                        I needed to do something similar, but for output directories in a data processing pipeline. I was inspired by Vorticity's answer, but added use of regex to grab the trailing number. This method continues to increment the last directory, even if intermediate numbered output directories are deleted. It also adds leading zeros so the names will sort alphabetically (i.e. width 3 gives 001 etc.)



                                                        def get_unique_dir(path, width=3):
                                                        # if it doesn't exist, create
                                                        if not os.path.isdir(path):
                                                        log.debug("Creating new directory - {}".format(path))
                                                        os.makedirs(path)
                                                        return path

                                                        # if it's empty, use
                                                        if not os.listdir(path):
                                                        log.debug("Using empty directory - {}".format(path))
                                                        return path

                                                        # otherwise, increment the highest number folder in the series

                                                        def get_trailing_number(search_text):
                                                        serch_obj = re.search(r"([0-9]+)$", search_text)
                                                        if not serch_obj:
                                                        return 0
                                                        else:
                                                        return int(serch_obj.group(1))

                                                        dirs = glob(path + "*")
                                                        num_list = sorted([get_trailing_number(d) for d in dirs])
                                                        highest_num = num_list[-1]
                                                        next_num = highest_num + 1
                                                        new_path = "{0}_{1:0>{2}}".format(path, next_num, width)

                                                        log.debug("Creating new incremented directory - {}".format(new_path))
                                                        os.makedirs(new_path)
                                                        return new_path

                                                        get_unique_dir("output")





                                                        share|improve this answer















                                                        I needed to do something similar, but for output directories in a data processing pipeline. I was inspired by Vorticity's answer, but added use of regex to grab the trailing number. This method continues to increment the last directory, even if intermediate numbered output directories are deleted. It also adds leading zeros so the names will sort alphabetically (i.e. width 3 gives 001 etc.)



                                                        def get_unique_dir(path, width=3):
                                                        # if it doesn't exist, create
                                                        if not os.path.isdir(path):
                                                        log.debug("Creating new directory - {}".format(path))
                                                        os.makedirs(path)
                                                        return path

                                                        # if it's empty, use
                                                        if not os.listdir(path):
                                                        log.debug("Using empty directory - {}".format(path))
                                                        return path

                                                        # otherwise, increment the highest number folder in the series

                                                        def get_trailing_number(search_text):
                                                        serch_obj = re.search(r"([0-9]+)$", search_text)
                                                        if not serch_obj:
                                                        return 0
                                                        else:
                                                        return int(serch_obj.group(1))

                                                        dirs = glob(path + "*")
                                                        num_list = sorted([get_trailing_number(d) for d in dirs])
                                                        highest_num = num_list[-1]
                                                        next_num = highest_num + 1
                                                        new_path = "{0}_{1:0>{2}}".format(path, next_num, width)

                                                        log.debug("Creating new incremented directory - {}".format(new_path))
                                                        os.makedirs(new_path)
                                                        return new_path

                                                        get_unique_dir("output")






                                                        share|improve this answer














                                                        share|improve this answer



                                                        share|improve this answer








                                                        edited Feb 8 at 20:34

























                                                        answered Feb 8 at 20:29









                                                        Woods26Woods26

                                                        11




                                                        11






























                                                            draft saved

                                                            draft discarded




















































                                                            Thanks for contributing an answer to Stack Overflow!


                                                            • Please be sure to answer the question. Provide details and share your research!

                                                            But avoid



                                                            • Asking for help, clarification, or responding to other answers.

                                                            • Making statements based on opinion; back them up with references or personal experience.


                                                            To learn more, see our tips on writing great answers.




                                                            draft saved


                                                            draft discarded














                                                            StackExchange.ready(
                                                            function () {
                                                            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f17984809%2fhow-do-i-create-a-incrementing-filename-in-python%23new-answer', 'question_page');
                                                            }
                                                            );

                                                            Post as a guest















                                                            Required, but never shown





















































                                                            Required, but never shown














                                                            Required, but never shown












                                                            Required, but never shown







                                                            Required, but never shown

































                                                            Required, but never shown














                                                            Required, but never shown












                                                            Required, but never shown







                                                            Required, but never shown







                                                            Popular posts from this blog

                                                            MongoDB - Not Authorized To Execute Command

                                                            How to fix TextFormField cause rebuild widget in Flutter

                                                            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith