Skip to content

Commit f6a2dfd

Browse files
author
Ishaq Khan
committed
file chapter
1 parent 6a00ce6 commit f6a2dfd

10 files changed

+105
-0
lines changed

Diff for: copingAndMovingFiles.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import shutil
2+
#copy file to a new folder
3+
fromLocation = ''
4+
toLocation = ''
5+
shutil.copy(fromLocation, toLocation + 'filename.extension') #adding a filename will rename the copied file
6+
7+
#copy a whole folder
8+
fromLocationofFolder = ''
9+
toLocationofFolder = ''
10+
shutil.copytree(fromLocationofFolder, toLocationofFolder)
11+
12+
#move file to newLocation
13+
14+
fromLocal = ''
15+
toLocal = ''
16+
shutil.move(fromLocal, toLocal)
17+
18+
19+
#moving to same place, but changing the extention of the file, will rename the file
20+
21+

Diff for: deletingFiles.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import os
2+
for filename in os.listdir():
3+
if filename.ends('.rxt'):
4+
print(filename)
5+
#can stop you from deleting the wrong file
6+
7+
#deleting files
8+
#1
9+
10+
filename = ''
11+
filepath = ''
12+
os.unlink(filename) #perma delete
13+
14+
#2 remove directory
15+
os.rmdir(filepath) #only if empty
16+
17+
#3 to delete a folder and everything inside
18+
import shutil
19+
shutil.rmtree(filepath) #perma deletes, wont be in the recylcing bin
20+
21+
#install = 'py -m pip install -U send2trash'
22+
import send2trash #sends to the recycling bin
23+
send2trash.send2trash(filename) # sends to the recycling bin
24+
25+

Diff for: makeFolder.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import os
2+
os.makedirs() #imput folder and it will create the folder
3+
#will create folders aswell depending on the string

Diff for: mockData/burger.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
I Love burgers
2+
And chips!

Diff for: mockData/hello.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This text has been modifiedAdded a second line to the file

Diff for: mockData/hello2.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Created new contentMore new contentCreated new contentMore new contentCreated new contentMore new contentCreated new contentMore new contentCreated new contentMore new contentCreated new contentMore new content

Diff for: mockData/myData.bak

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
'names', (0, 56)

Diff for: mockData/myData.dat

56 Bytes
Binary file not shown.

Diff for: mockData/myData.dir

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
'names', (0, 56)

Diff for: readingAndWritingPlainText.py

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
path = 'C:\\Users\\ikhan\\Documents\\learningPython\\mockData\\hello.txt' #have to use double quotes
2+
helloFile = open(path)
3+
helloFile.read() #can only read once
4+
content helloFile.read() # can call it multiple times
5+
helloFile.close()
6+
7+
#readlines
8+
#returns the contents as lines in a list
9+
listHelloFile = helloFile.readlines()
10+
helloFile.close()
11+
12+
#Write mode (override current file )
13+
#if file doesn't exist python will create a new file
14+
#same with append
15+
16+
helloFile = open(path, 'w') #second parameter is w for write
17+
helloFile.write('This text has been modified') #changes the txt file
18+
helloFile.write('Added a second line to the file')
19+
helloFile.close()
20+
21+
#append mode
22+
newPath = path.replace('hello', 'hello2')
23+
helloFile = open(newPath, 'a')
24+
helloFile.write('Created new content')
25+
helloFile.write('More new content')
26+
helloFile.close()
27+
28+
#create new file
29+
import os
30+
os.chdir('C:\\Users\\ikhan\\Documents\\learningPython\\mockData') #changing the location of where the file will be saved to
31+
burgerFile = open('burger.txt', 'w')
32+
burgerFile.write('I Love burgers \n And chips!')
33+
burgerFile.close()
34+
35+
#Storing complex variables using the shelve module
36+
import shelve
37+
shelveFile = shelve.open('myData') #location is based of os.chdir
38+
shelveFile # a dictionary-like data structure
39+
shelveFile['names'] = ['ishaq' , 'fred' , 'batman', 'sunny']
40+
shelveFile.close()
41+
42+
#in the future can open the file and close it, like a dictionary that saves else where
43+
keys = list(shelveFile.keys())
44+
values = list(shelveFile.values())
45+
46+
47+
48+
49+
50+

0 commit comments

Comments
 (0)