Page 1 of 1

Making a File Number module

#1 Eric115  Icon User is offline

  • coderさん
  • member icon

Reputation: 63
  • View blog
  • Posts: 697
  • Joined: 19-January 09

Posted 12 July 2010 - 07:57 PM

Sometimes when you write a program, you want to allow the program to have default file/folder names that won't clash with other files when the program creates it. A good example of this is in a lot of programs when you go to save something, it comes up as Untitled1 or if you already have Untitled1, it comes up with Untitled2.
I'm going to show you how to write a module which can be used to tell the program which number hasn't been used.

The module should work something like this;
It has a main(path, FileName) function which asks the caller to pass it where the files are stored and what the name of the file is (without the number on the end of it). When this is passed to the module this should happen;
1. The module tests to see if the path exists
2. If the path exists, it begins a while loop to only stop when
done = True
3. In the while loop, the program will start a 'for' loop to go through and test if the File/folder name with the number on the end exists using the os module.
4. If the file/folder with a number on the end exists (e.g. Name1),
the module will add 1 to a variable which is keeping track of how many numbers it has checked.
5. If the file/folder name with a number on the end doesn't exist, it stops all of the loops and returns the number it was up to back to the caller. (e.g. 2 as in Name2 doesn't exist).
6. If the first path it tests doesn't exist, it returns an Error

So, coded out it should look something like this;
import os
def Main(path, FileName):
    MaxNumb = 100#sets the maximum number to be searched for.
    if os.path.exists(path+"\\"+FileName):
        done = False
        Num = 1#the file number it has check up to
        while not done:
            for i in range(0, MaxNumb): #starts the for loop
                TestNum = str(Num) #Makes Num a string
                TestFile = os.path.exists(path+"\\"+FileName+TestNum)
                #Changes the file name to check for each time
                if TestFile == True:
                    Num = Num + 1 #Adds 1 to the number var.
                if TestFile == False:
                    done = True
                    return Num
                    #sends the caller the current Num var
    else:
        return "File does not exist!"
        #Sends the caller an error



Ok so now lets try this module and see if it works.
Either copy the code above and save it into a file or download the attached file and copy it to "C:\Python25\Lib\site-packages"
(Python folder may be different in different versions of Python).
Then find somewhere on your computer and make a folder(call it whatever you want). Then inside of that folder make three new folders;
Name, Name1 and Name2.
When all this is done, open up an interactive Python window and import the module (I called mine FileNumber)

>>> Import FileNumber
>>> Number = FileNumber.Main('C:\Users\Eric\Documents\Python', 'Name')
>>> print Number
>>> 3
This means that you do not have a folder called Name3. That is what should happen if the module doesn't have an error. This is what the output will look like if an error occurs:

>>> Import FileNumber
>>> Number = FileNumber.Main('C:\Users\Eric\Documents\Python', 'Name')
>>> print Number
>>> File does not exist!

That's how you make a file numbering module!

This was my first tutorial so any suggestions or constructive criticism is welcomed! Thanks.

Attached File(s)



Is This A Good Question/Topic? 1
  • +

Replies To: Making a File Number module

#2 Eric115  Icon User is offline

  • coderさん
  • member icon

Reputation: 63
  • View blog
  • Posts: 697
  • Joined: 19-January 09

Posted 09 August 2010 - 01:34 AM

Update:
You could probably work this out from what I have given you above, but just in case anyone wanted to know this is how to get a number without having to have a file with no number e.g. instead of having file, file1, file2 you can just have file1,file2 ect.
import os
def NewNumb(path, FileName):
    MaxNumb = 100
    if os.path.exists(path):
        done = False
        Num = 1
        while not done:
            for i in range(0, MaxNumb):
                TestNum = str(Num)
                TestFile = os.path.exists(path+"\\"+FileName+TestNum)
                if TestFile == True:
                    Num = Num + 1
                if TestFile == False:
                    done = True
                    return Num
    else:
        return "File does not exist!"


Yea, I know easy to work out but this might help someone so it was worth posting (I hope it was anyway) :turned:
Was This Post Helpful? 0
  • +
  • -

#3 jennifergeor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 05-November 11

Posted 07 November 2011 - 09:13 AM

I think, that you are not right. I can defend the position. Write to me in PM.
Was This Post Helpful? 0
  • +
  • -

#4 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3730
  • View blog
  • Posts: 6,017
  • Joined: 08-June 10

Posted 07 November 2011 - 05:33 PM

Why don't you explain it here, so we can all be a part of the discussion.
Was This Post Helpful? 0
  • +
  • -

#5 charles314  Icon User is offline

  • New D.I.C Head

Reputation: -11
  • View blog
  • Posts: 18
  • Joined: 13-September 12

Posted 13 September 2012 - 08:21 PM

Why not "from os import path" instead of the entire os module?

Why not "from os import path" instead of the entire os module?
Was This Post Helpful? 0
  • +
  • -

#6 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Posted 17 September 2012 - 12:40 PM

View Postcharles314, on 13 September 2012 - 11:21 PM, said:

Why not "from os import path" instead of the entire os module?
Despite what the convention may appear to suggest, you are still importing the entire os module either way... sort of. This should at least explain that the difference in performance and memory usage are going to be minimal.

This post has been edited by atraub: 17 September 2012 - 01:03 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1