Social Icons

Thursday, March 5, 2020

Exercises on Python Files I/O With Solutions

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

No comments:

Post a Comment

Category Of Mobile Courses

Actualités (644) Adsense (1) Affiliation (1) Algebraic Topology (1) Algorithmic (1) all-news (30) Android (5) Android App (8) Android app without code (4) Android Apps (167) Android Development (4) Android download (2) Android OS (3) AngularJS (1) Automata theory and formal language (5) C programming (5) Category and Functor (8) CMS (3) Computer Glossary (18) Create Mobile App With Ionic Framework (2) CSS (2) CSS-Cascading-Style-Sheets (4) Developpement Java (13) Differential Geometry (1) Django-Python-Framework (8) dropshiping (26) Earn Money by Internet (4) Emplois (23) Framework php (2) Fraud (2) HTML (7) Java For Beginners (10) Javascript (12) Kotlin Programming Language (8) Kotlin For Mobile Android (1) Linux Download (2) Marketing (5) Mobile (3) Mobile Courses (4) Mobile Marketing (4) MoneyGram (1) News (721) Node.js (5) Open Source (1) Photoshop (1) Protect Computer (1) Python (32) Python BeautifulSoup (1) Python For Data Science (2) Python-Books (6) Python-DVD-Training (1) Python-Exercises (130) Python-Framework (1) Python-IDE (1) Python-Modules (1) Python-pdf (2) Python-pyQt (1) Référencement (2) Script PHP (2) Security (6) SEO (1) Snipping Tool: Faq (1) Social Networks (1) Source Code (1) Statistics With SPSS (2) Surveillance Software (1) Travail à domicile (6) Tutoriels php en vidéos (2) Tutoriels-MySql (6) tutoriels-php (19) Utilitaires (1) VPS (1) Web Hosting (1) Webcam (1) Webmarketing (11) Western Union (1) Windows 10 (1) Windows 7 (4) Windows 7 Faq (2) Windows 8 (1) Windows Accessories (1) Windows Download (8) Windows Drivers (1) Windows Fonts (1) Windows Power Shell (2) Windows Registry (2) Windows Security (18) Windows Software (2) Windows Spyware (2) Windows utilities (3) Windows Virus (2) Windows Vista (3) Windows Wireless (1) Windows xp (1) Wordpress (1)
 

Sample text

Sample Text

 
Blogger Templates