Exercise 49  ||  Solution

  1. Write a Python program that allows you to create a file on desktop called myFile.txt and write on it the content s = 'Python is oriented programming language'. You must at first retrieve the username via the os.getlogin() method.
  2.  write a Python program to read the existing file on the desktop myFile.txt.

Exercise 50

Using the os and chdir modules in console mode, give the commands that allow you to create a file called myFile.txt on the desktop.

Exercise 51  ||  Solution

Using the os.rename() method, create a python program to rename an existing file on the desktop called myfile.txt to myfile2.txt.

Exercise 52 || Solution

  1.  Write a python program that allowing you to create a directory in the desktop called myDir
  2. Write  a Python program allowing you to create a file in the desktop named myFile.txt and write it the following lines:
    here is an example of a text file
    this file was created with python
    we can write on this file
  3. Write  a Python program allowing you to moving myFile.txt in the  directory  myDir.

Exercise 53 || Solution

Create manually  two files in the same directory:

  • python_code.py
  • myfile.txt

1) - Create a Python program that writes the following three lines to the myfile.txt file:
First line
second line
third line
2) - Write a Python program on python_code.py that  replace the second line with the line:  "sorry! The content of this line has been changed! "

Exercise 54 || Solution

Write a program in Python which allows you to insert at the 3rd position of an existing file called myFile.txt, the line "This line was inserted via Python code! " without changing the existing content file.

Exercise 55 || Solution

1) Write a Python program that create a txt file called myfile.txt and write on it the texte: "Python is object oriented programming language".
2) Write an ohter python program that removes the 3rd word from this file.

Exercise56 || Solution

Given a file called myfile.txt which contains the following lines:

line 1
line 2
line 3

write a Python program that transforms the content into the form:

line 3
this line has just been inserted via Python code
line 2
line 1

Exercise57 || Solution

Given a file called myfile.txt which contains the following text:
"Python is object oriented programming language".
Write a program in Python that transforms the content of the file by writing each word in a separate line.

Exercise58 || Solution

a) Write a program that lists all the folders in the 'C: / Windows' directory
b) write another program which lists all the files in the 'C: / Windows'  directory.
c) Using the getlogin() method, write a program that make the same operations for the user's Desktop directory.

Exercise 59 || Solution

Write a Python program that displays the longest word found in a text file.

Exercise 60  ||  Solution

Write a function in python that allows you to count the frequency of repetition of each word found in a given file.

Exercise 61 || Solution

Write a program in Python allowing to delete multiple spaces in a text file named myfile.txt which contains a text: T = 'Python        is         programming         language'

Exercise 62 || Solution

a)Write a python program that create a text file called myFile.txt and write on it the following lines:

- this is the line 1

- thtis is the line 2

- this is the line3

- this is the line4

- this is the line5 

- this is the line6

- this is the line7

- this is the line8

- this is the line9

- this is the line10    

b) Write a Python program which allows you to read the first 5 lines of myFile.txt.
c) Write a program to read the last 5 lines of myFile.txt
d) Write a Python program which allows you to extract the content of a file from the 3rd line to the 7th line and save it in another file called extract_content.txt.

Exercise 63

Write a Python program that allows you to group in a list the words common for two text files: file1.txt and file2.txt.

Exercise 64

Write a program in Python that allows you to align right the content of an html file.

Exercise 65 || Solution

Write an algorithm in python in the form of a function which takes as argument the path of an existing file and returns the number of lines of this file.

Exercise 66 || Solution

Write an algorithm in python which asks the user to type his name, his age, his email and creates a file named personal_informations.txt containing all the information entered. Example if the informations entered is: David, 37 years old, david37@gmail.com, the personal_information.txt file must contain the following information:
Name: David.
Age: 37 years old.
Email: david37@gmail.com

Younes Derfoufi
my-courses.net

Leave a Reply