[Tutor] Need help with saving files to a specific folder...

Michael P. Reilly arcege@shore.net
Fri, 23 Mar 2001 07:43:24 -0500 (EST)


> I need help with saving a file to a specific folder
> like this:
> #######################################################
> List = []
> 
> def save_list(numbers,filename):
>     out_file = open(filename,'w')
>     out_file.write(get_slip())
>     out_file.close()
> 
> def get_slip():
>     return '''
>     ___________________________
>     %s     %s
>     EMP #       CAT         IN       OUT
>     %s           %s            %s        %s
>                                IN       OUT
>                                %s       %s
>     ------------------------------------------
>     ''' % (Name, Date, Empnum, CAT, FIRSTIN, FIRSTOUT,
> SECIN, SECOUT)
> 
> Name = raw_input("What is the employees name: ")
> Date = raw_input("What is the Date: ")
> Empnum = input("What is the Employees Number: ")
> CAT = input("What is the CAT number: ")
> FIRSTIN = raw_input("What time did (he, she) come in:
> ")
> FIRSTOUT = raw_input("What time did (he, she) go on
> break: ")
> SECIN = raw_input("What time did (he, she) come in
> from break: ")
> SECOUT = raw_input("What time did (he, she) clock out
> to go home: ")
> 
> print get_slip
> filename = raw_input('What do you want to call the
> file: ')
> save_list(List,filename+'.txt')
> ######################################################
>   I am want to save the file to a folder called
> Times so that my program will be organized instead
> of clumped up all in one place.
> 
>   This is the setup that i want:
> MainFolder
>      program
>      dlls
>      SubFolder
>           saved files
> 
>   I cant figure out how to do this

There are routines in the os.path module to allow you to manipulate
pathnames for your platform.

Specifically you want the os.path.join() function:
  save_list(List, os.path.join('Times', filename + '.txt'))

And to make sure the directory exists before you attempt to write
there:
  if not os.path.isdir('Times'):
    os.mkdir('Times')

  -Arcege

References:
Python Library Reference, sec 6.2 os.path -- Common pathname manipulations
  http://www.python.org/doc/current/lib/module-os.path.html

-- 
------------------------------------------------------------------------
| Michael P. Reilly, Release Manager  | Email: arcege@shore.net        |
| Salem, Mass. USA  01970             |                                |
------------------------------------------------------------------------