Author Topic: Question: Help my understanding of my code  (Read 1088 times)

0 Members and 1 Guest are viewing this topic.

Offline Moistfish

  • Serf
  • *
  • Posts: 20
  • Cookies: 1
    • View Profile
Question: Help my understanding of my code
« on: January 11, 2014, 03:54:15 pm »
Hello,  learning python atm and came across this below.  Wanted to know if anyone knew why this happened


This one didn't write anything to the test.txt file


Code: [Select]
writeFile = open(r'C:/Documents and Settings/MyComp/Desktop/Python/test.txt', 'w')

def main():
   
   name1 = "Joe"
   name2 = "James"
   name3 = "Johnny"

   writeFile.write(name1)
   writeFile.write(name2)
   writeFile.write(name3)

   writeFile.close

main()



and this one did




Code: [Select]
def main():
   
   writeFile = open(r'C:/Documents and Settings/MyComp/Desktop/Python/test.txt', 'w')

   name1 = "Joe"
   name2 = "James"
   name3 = "Johnny"

   writeFile.write(name1)
   writeFile.write(name2)
   writeFile.write(name3)

   writeFile.close

main()


I obviously fixed it, but the question is more for my understanding why it works and writes to the test file when the open command is called within the function, rather than outside it.


Thanks for the help =)
« Last Edit: January 11, 2014, 03:54:49 pm by Moistfish »

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: Question: Help my understanding of my code
« Reply #1 on: January 11, 2014, 04:17:05 pm »
In the first block of code "writeFile" is considered as a global variable. Python, as well as PHP, have a security measure from accidental usage of global variables. Try adding "global writeFile" just below the function definition (below the DEF line).

Offline s3my0n

  • Knight
  • **
  • Posts: 276
  • Cookies: 58
    • View Profile
    • ::1
Re: Question: Help my understanding of my code
« Reply #2 on: January 11, 2014, 04:26:45 pm »
It's not "writeFile.close", it is "writeFile.close()" since it is a function.
Easter egg in all *nix systems: E(){ E|E& };E

Offline vezzy

  • Royal Highness
  • ****
  • Posts: 771
  • Cookies: 172
    • View Profile
Re: Question: Help my understanding of my code
« Reply #3 on: January 11, 2014, 04:27:11 pm »
In the first block of code "writeFile" is considered as a global variable. Python, as well as PHP, have a security measure from accidental usage of global variables. Try adding "global writeFile" just below the function definition (below the DEF line).

Are you sure it's a security measure or simply how scoping works in Python?
Quote from: Dippy hippy
Just brushing though. I will be semi active mainly came to find a HQ botnet, like THOR or just any p2p botnet

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: Question: Help my understanding of my code
« Reply #4 on: January 11, 2014, 07:04:06 pm »
Are you sure it's a security measure or simply how scoping works in Python?
heh yes, that too.

Offline Moistfish

  • Serf
  • *
  • Posts: 20
  • Cookies: 1
    • View Profile
Re: Question: Help my understanding of my code
« Reply #5 on: January 11, 2014, 07:24:29 pm »
It's not "writeFile.close", it is "writeFile.close()" since it is a function.


Yea thanks, typo when repeating the code.  Originally wrote the program on my laptop, wrote this post on my main PC.


Thanks guys =) i shall go and check out scoping in python