Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- """
- Created on Thu Mar 12 22:12:36 2020
- The function isWordInFile(fileName, word), which
- given a string, filename, and a string, word, checks whether or not word is in the file
- named fileName.
- @author Abdallah Wahidi
- """
- """
- Checks whether or not 'word' is in the file named 'fileName'
- """
- def isWordInFile(fileName, word):
- nameHandle = open(fileName, 'r')
- s = nameHandle.read()
- if word in s:
- print('True')
- else:
- print('False')
- nameHandle.close()
- isWordInFile("test.txt", "Programming")
- isWordInFile("test.txt", "programming")
- """
- A variation of Part (a) which requires
- reading the file line by line
- """
- def wordSearch(fileName, word):
- nameHandle = open(fileName, 'r')
- i = 1
- for line in nameHandle:
- if word in line:
- print(i)
- i+=1
- nameHandle.close()
- wordSearch("test.txt", 'Programming')
- """
- Part C:the function duplicateLines(fileName), which given a string
- fileName, opens the file named fileName for reading and creates a new file whose
- content is like fileName but with all lines duplicated
- """
- def duplicateLines(fileName):
- assert ".txt" in fileName, "The file is not a txt file"
- nameHandle = open(fileName, 'r')
- newFile = open("nameDuplicated.txt", 'w')
- for line in nameHandle:
- newFile.write(line)
- newFile.write(line)
- nameHandle.close()
- newFile.close()
- duplicateLines("test.txt")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement