I need some advice

Discuss how to write good code, break bad code, your current pet projects, or the best way to approach novel problems

I need some advice

Post by Master1243 on Mon Feb 08, 2016 9:28 pm
([msg=91532]see I need some advice[/msg])

I am a newbie and I have been studying python on codecademy. I am getting pretty good at it, but I am just tired of doing THEIR missions, so I downloaded the python software, but I don't know how to save files. If someone could help me.....that would be great

Thank You.
Master1243
New User
New User
 
Posts: 2
Joined: Mon Feb 08, 2016 9:25 pm
Blog: View Blog (0)


Re: I need some advice

Post by cyberdrain on Mon Feb 08, 2016 10:09 pm
([msg=91533]see Re: I need some advice[/msg])

If you mean opening files for reading/writing use:
Code: Select all
f = open('<path to file>', 'r')
--or--
f = open('<path to file>', 'w')

As for saving code, just open an editor (notepad, notepad++, vi, emacs) and save the file. Then run it on the command line with:
Code: Select all
python <path to file>
Free your mind / Think clearly
User avatar
cyberdrain
Expert
Expert
 
Posts: 2160
Joined: Sun Nov 27, 2011 1:58 pm
Blog: View Blog (0)


Re: I need some advice

Post by -Ninjex- on Mon Feb 08, 2016 10:58 pm
([msg=91535]see Re: I need some advice[/msg])

You normally just close the file after opening in write mode (W) and closing it. Python will also go ahead and save the file for you. There is no need to explicitly call a save function on it or anything.
These two following example will create a file "data.txt" and write "my secret data" to the file and close it (also saving it)

Code: Select all
with open('data.txt', 'w') as my_file:
    my_file.write('my secret data')


Code: Select all
my_file = open('data.txt', 'w')
my_file.write('my secret data')
my_file.close()
image
For those that know
K: 0x2CD8D4F9
User avatar
-Ninjex-
Moderator
Moderator
 
Posts: 1691
Joined: Sun Sep 02, 2012 8:02 pm
Blog: View Blog (0)


Re: I need some advice

Post by cyberdrain on Tue Feb 09, 2016 11:34 am
([msg=91544]see Re: I need some advice[/msg])

Heh, you might as well put it into a try clause while you're at it, just to make sure the script doesn't fail completely if writing isn't possible. ;) But yeah, it's always a good idea to close the file when you're done with it...
Free your mind / Think clearly
User avatar
cyberdrain
Expert
Expert
 
Posts: 2160
Joined: Sun Nov 27, 2011 1:58 pm
Blog: View Blog (0)


Re: I need some advice

Post by -Ninjex- on Tue Feb 09, 2016 1:12 pm
([msg=91545]see Re: I need some advice[/msg])

cyberdrain wrote:Heh, you might as well put it into a try clause while you're at it, just to make sure the script doesn't fail completely if writing isn't possible. ;) But yeah, it's always a good idea to close the file when you're done with it...


<3
Code: Select all
try:
    with open("data.txt", "w") as my_file:
        my_file.write('my secret data')
except IOError:
    print '[!] Could not open the file for writing!'


I also just noticed this was in the wrong area and moved it accordingly. OP, please be weary that we have two areas for programming on the forums. One section is related to the HTS Programming Challenges and the other is for general programming questions.
image
For those that know
K: 0x2CD8D4F9
User avatar
-Ninjex-
Moderator
Moderator
 
Posts: 1691
Joined: Sun Sep 02, 2012 8:02 pm
Blog: View Blog (0)


Re: I need some advice

Post by cyberdrain on Wed Feb 10, 2016 8:46 pm
([msg=91568]see Re: I need some advice[/msg])

-Ninjex- wrote:<3
Code: Select all
try:
    with open("data.txt", "w") as my_file:
        my_file.write('my secret data')
except IOError:
    print '[!] Could not open the file for writing!'

Nice! :)
Free your mind / Think clearly
User avatar
cyberdrain
Expert
Expert
 
Posts: 2160
Joined: Sun Nov 27, 2011 1:58 pm
Blog: View Blog (0)



Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests