[Tutor] creating files

Liam Clarke cyresse at gmail.com
Mon Nov 22 07:46:46 CET 2004


> I'm sure there is a more elegant way of doing it since I am a non-programmer
> beginner trying to learn Python for the first time.

I've been at it six weeks, and I've nearly finished my first app, gui et al.

If you're using Windows or DOS - 

You could simply do - 

dirPath = raw_input(" Please enter directory to save to: (in format
c:\dave\ha\)")
fileName = raw_input("Please enter filename : ")

dirPath = dirPath.replace("\","/")
if  not dirPath.endswith("/") :
        filePath=dirPath+"/"+fileName
else:
        filePath=dirPath+fileName

saveFile = (filePath, "w")



Raw input is the key. Please note that the above is very awkward, and
Windows specific.
( Because os.path.join does funny stuff using 'current directories',
which I don't understand )

For cross-platform compatibility, explore os.path.join, and then
explain it to me when you get it : )

So, the user enters the directory to save to - 'C:\bob\marley\reggae\Man'

and the filename '3lilbird.son'

Now, dirPath.replace("\","/") does just that. It turns the directory into

'C:/bob/marley/reggae/Man'

Python uses it like that, I guess it's a cross platform thing.

It then checks whether or nor the user ended the directory with a '\'
(which would now be a '/'), if not, it adds it before cocentating the
filename

The string methods are pretty logical, and very useful. 


> Thank you for your help.
> 
> Oh, 1 other question though.  It would enhance the program if the
> interfacing user could type in the path and names of the files they want to
> use rather than use already created files.  Is there an easy way to do that?
> Worth extra points.
> 
> Thanks,
> 
> 
> 
> Jim
> 
> -----Original Message-----
> From: Liam Clarke [mailto:cyresse at gmail.com]
> Sent: Sunday, November 21, 2004 2:59 AM
> To: Python Tutor; jdecaro2 at comcast.net
> Subject: Re: [Tutor] creating files
> 
> Hi Jim,
> 
> I'm glad it worked.
> 
> I've forwarded this to the list because someone else usually posts a
> more elegant way to do something.
> 
> Have fun,
> 
> Liam Clarke
> 
> On Sun, 21 Nov 2004 00:36:30 -0500, Jim DeCaro <jdecaro2 at comcast.net> wrote:
> > THANK YOU IT WORKED!!!!!!!!
> >
> >
> >
> > Jim
> 
> ---------- Forwarded message ----------
> From: Jim DeCaro <jdecaro2 at comcast.net>
> Date: Sun, 21 Nov 2004 00:24:21 -0500
> Subject: RE: [Tutor] creating files
> To: Liam Clarke <cyresse at gmail.com>
> 
> Thank you for responding so quickly.
> 
> Here is what I have so far....
> 
> I will try your logic and see what happens.
> 
> Thanks, I'll let you know how it works, and if you want to do anything with
> this code, feel free.
> 
> Jim
> 
> -----Original Message-----
> From: Liam Clarke [mailto:cyresse at gmail.com]
> Sent: Saturday, November 20, 2004 11:36 PM
> To: jdecaro2 at comcast.net; Python Tutor
> Subject: Re: [Tutor] creating files
> 
> Hi Jim,
> 
> Are you able to post your code?
> 
> So, you've got a loop (this is pseudo-code)
> 
> for element in inputString:
>       outputLetter = element
>       if element in compareToList:
>                 outputLetter = otherListItem
>        print outputLetter
> 
> You could just do this -
> 
> saveStringList=[]
> 
> for element in inputString:
>      outputLetter = element
>       if element in compareToList:
>                 outputLetter = otherListItem
>        print outputLetter
>         saveStringList.append(outputLetter)
> 
> saveString="".join(saveStringList)
> 
> outputFile=file('encrypted.bit','w')
> outputFile.write(saveString)
> outputFile.close()
> 
> So saveStringList starts as []
> 
> and each time through the loop, your output letter is printed to the
> screen, and then appended to saveStringList.
> 
> So, if the word 'back' became 'head' when encrypted, the prog would
> flow like this -
> 
> saveStringList would become ['h'] then ['h','e'] and so forth until
> your loop finished and it was ['h','e','a','d']
> 
> Then saveString="".join(saveStringList) just concentates the list
> elements into the string 'head'.
> 
> Which you can then write happily to disk.
> 
> Hope it helps.
> 
> (I use a list to remove the hassle of globals.)
> 
> Liam Clarke
> 
> On Sat, 20 Nov 2004 23:36:23 -0500, Jim DeCaro <jdecaro2 at comcast.net> wrote:
> > I am very new, and am writing a sketchy program to do a simple encryption
> > for a school project.  I have created a loop that iterates through letters
> > from a read file, and compares them to a list.  When it finds a match, it
> > converts the letter to a another letter from a corresponding list (hence
> the
> > encryption) and prints it to the screen.  The logic works fine, but I
> can't
> > save the output to a file.  I will not necessarily know what is in the
> file
> > when it is read, so I can't tell the program to write specific known data
> as
> > in the tutorials. The output comes from iterations.  How do I make a
> > variable that contains all of the letters combined in 1 string that can be
> > saved to a file? I can sometimes get it to write 1 of the letters to the
> > file.
> >
> > I have not gotten to functions yet, so I was hoping there would be a
> method
> > to save each iterated letter to a variable. I have seen the append method,
> > but I'm not sure about it.
> >
> > Thanks,
> >
> > Jim
> >
> > _______________________________________________
> > Tutor maillist  -  Tutor at python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
> 
> --
> 'There is only one basic human right, and that is to do as you damn well
> please.
> And with it comes the only basic human duty, to take the consequences.
> 
> --
> 'There is only one basic human right, and that is to do as you damn well
> please.
> And with it comes the only basic human duty, to take the consequences.
> 
> 


-- 
'There is only one basic human right, and that is to do as you damn well please.
And with it comes the only basic human duty, to take the consequences.


More information about the Tutor mailing list