title: Using loop to write in files
body: <p>I am trying to download the raw code from all the tutorials I've been watching, so I made this:</p>
  
  <pre><code>import requests
  from urllib import request
  from bs4 import BeautifulSoup
  
  page_url='https://github.com/buckyroberts/Source-Code-from-
  Tutorials/tree/master/Python'
  
  def page(main_url):
      code=requests.get(main_url)
      text=code.text
      soup=BeautifulSoup(text, "html.parser")
      for link in soup.findAll('a', {'class': 'js-navigation-open'}):
          code_url='https://github.com'+link.get('href')
          codelist(code_url)
  
  def codelist(sec_url):
      code = requests.get(sec_url)
      text = code.text
      soup = BeautifulSoup(text, "html.parser")
      for link in soup.findAll('a', {'id': 'raw-url'}):
          raw_url='https://github.com'+link.get('href')
          rawcode(raw_url)
  
  def rawcode(third_url):
      response = request.urlopen(third_url)
      txt = response.read()
      lines = txt.split("\\n")
      dest_url = r'go.py'
      fx = open(dest_url, "w")
      for line in lines:
          fx.write(line + "\n")
      fx.close()
  
  page(page_url)
  </code></pre>
  
  <p>When I run this code am expecting to create 40 py files consisting of 40 different codes from here-<a href="https://github.com/buckyroberts/Source-Code-from-Tutorials/tree/master/Python" rel="nofollow noreferrer">https://github.com/buckyroberts/Source-Code-from-Tutorials/tree/master/Python</a>
  But it doesn't work. Twice, it randomly chose to download just one of the 40 files. Like this-</p>
  
  <p>The first two functions work well together until the third one is called. But the third works fine alone.</p>
  
  <p>I started learning Python 4 days back, any help will be much appreciated. Thank you, guys!</p>