Exercise 52
- Write a python program that allowing you to create a directory in the desktop called myDir
- 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 - Write a Python program allowing you to moving myFile.txt in the directory myDir.
Solution
Question1
import os
# get user name
user = os.getlogin()
#getting the desktop directory
d = "C:/users/" + user + "/desktop/"
# creating directory myDir in the desktop
os.mkdir(d + "myDir/")
Question2
import os
# get user name
user = os.getlogin()
#creating myFile.txt in the desktop
myfile = "C:/users/" + user + "/Desktop/myFile.txt"
#opening myFile.txt in writing mode w
f = open(myfile , 'w')
#writing on myFile.txt
f.write("here is an example of a text filen this file was created with pythonn we can write on this file")
f.close()
Question3
user = os.getlogin()
#getting the desktop directory
# moving myFile.txt to myDir directory
os.rename("C:/Users/" +user + "/Desktop/myFile.txt" , "C:/Users/" +user + "/Desktop/myDir/myFile.txt")
Younes Derfoufi
my-courses.net
my-courses.net