#!/usr/bin/env python
# coding: utf-8

# In[1]:


import os
import re


# In[2]:


#problem 1
my_filepath = os.path.join('baby-names', 'Most_Popular_Baby_Names__1980-2013.csv' )

def calc_file_length(my_filepath):       
    if not (os.path.isfile(my_filepath)):        
        return False
    else:
        file_length = 0
        with open(my_filepath) as data_file:
            for line in data_file:
                file_length += 1        
        return file_length
  
    


# In[3]:


file_length = calc_file_length(my_filepath)
print(file_length)


# In[4]:


#problem 2
temp_filepath = os.path.join('temperature.txt' )

def parse_weather_data_file(my_tmp_filepath):
    if not (os.path.isfile(my_tmp_filepath)):        
        return False
    else:
        tmp_reading = []
        with open(my_tmp_filepath) as data_file:
            for line in data_file:
                curr_reading = {}                
                line = line.strip()             
                station_id = line[0:8]
                if (station_id != ''):
                    curr_reading = {
                        'station_id' : station_id,
                        'temperature' : line[8:11],
                        'relative_humidity':line[11: len(line)]
                    }
                    tmp_reading.append(curr_reading)
                
                #print(line[0:5])
        
        return tmp_reading
    


# In[5]:


tmp_reading = parse_weather_data_file(temp_filepath)
print(tmp_reading)


# In[6]:


#problem 3
word_filepath = os.path.join('words.txt' )

def called_word_count(my_wordcount_filepath):
    if not (os.path.isfile(my_wordcount_filepath)):        
        return False
    else:
        word_counts = {}
        with open (my_wordcount_filepath) as data_file:
            for line in data_file:                
                line = re.sub("[,.]","", line.strip())                
                word_arrray = line.split(' ')
                for word in word_arrray:                   
                    if word not in word_counts:
                        word_counts[word] = 1
                    else:
                        word_counts[word] += 1                                
            
        return word_counts
    


# In[7]:


word_counts = called_word_count(word_filepath)
print(word_counts)


# In[8]:


#problem 4
new_filepath = os.path.join('newfile.txt')

def write_to_file(path_to_new_file, lines):
    with open(path_to_new_file, 'w') as myNewFile:
        for line in lines:
            myNewFile.write(line + ' ')           
    
    


# In[9]:


write_to_file(new_filepath,['This', 'is', 'content', 'of', 'new', 'file'])


# In[10]:


#problem 5
org_word_filepath = os.path.join('original_words.txt' )
copied_word_filepath = os.path.join('copied_words.txt' )

def copy_file(path_to_existing_file, path_coppied_to_file):
    with open(path_to_existing_file, 'r') as myReadFile, open(path_coppied_to_file,'w') as myWriteFile:
        for line in myReadFile:
            myWriteFile.write(line)                
        myReadFile.close()


# In[11]:


copy_file(org_word_filepath, copied_word_filepath)


# In[ ]:




