Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-robot77 authored Jul 6, 2023
1 parent 0bc9b1e commit 9d2a83d
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Python/ch4.File Handling/Work with OS.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
commands:
- getcwd() # getting pointer address
- chdir() # change pointer address
- listdir() # contents of current address like as: list of all files and folders
- mkdir() # creating new folder
- rmdir() # removing an empty folder
- remove() # removing a file
- rename() # transfer a file to another directory
- system() # running directly system commands in python terminal
- walk() # finding all files of a folder
- path.exists() # existense of a file/folder
- path.isfile() # found is input a file?
- path.isdir() # found is input a folder?
- path.basename() # giving file name of input address
- path.join() # concat of some strings of name from input to output
- path.commonprefix() # giving longest common prefix of input in output
8 changes: 8 additions & 0 deletions Python/ch4.File Handling/Work with OS_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
>>> import os
>>> os.getcwd()
'/home/mohamadreza/Desktop/python/os_sample'
>>> os.listdir()
['codes', 'README.md', 'data', 'images']
>>> os.chdir('codes')
>>> os.listdir()
['test.py', 'project', 'model.py', 'main.py']
8 changes: 8 additions & 0 deletions Python/ch4.File Handling/Work with OS_10.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
>>> os.path.commonprefix(['project/test.py', 'project/main.py'])
'project/'
>>> os.path.commonprefix(['project/data/test.py', 'project/data/main.py'])
'project/data/'
>>> os.path.commonprefix(['project/data/test.py', '../main.py'])
''
>>> os.path.commonprefix(['project/data','modules/data'])
''
12 changes: 12 additions & 0 deletions Python/ch4.File Handling/Work with OS_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
>>> os.mkdir('images/other')
>>> os.mkdir('images/other/2019')
>>> os.listdir('images/other')
['2019']
>>> os.rmdir('images/other')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: [WinError 145] The directory is not empty:
'/home/ali/Desktop/python/os_sample/images/other'
>>> os.rmdir('images/other/2019')
>>> os.listdir('images/other')
[]
4 changes: 4 additions & 0 deletions Python/ch4.File Handling/Work with OS_3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
>>> os.remove('model.py')
>>> os.listdir()
['project', 'test.py', 'main.py']
>>> os.remove('../images/animal/ant.png')
12 changes: 12 additions & 0 deletions Python/ch4.File Handling/Work with OS_4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
>>> os.rename('test.py', 'model.py')
>>> os.listdir()
['project', 'model.py', 'main.py']
>>> os.rename('model.py', 'project/model.py')
>>> os.listdir()
['project', 'main.py']
>>> os.rename('main.py', 'project/a.py')
>>> os.listdir()
['project']
>>> os.listdir('project')
['control.py', 'a.py', 'view.py', 'model.py']
>>> os.system('python codes/main.py')
7 changes: 7 additions & 0 deletions Python/ch4.File Handling/Work with OS_5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
>>> w = os.walk('.')
>>> type(w)
<class 'generator'>
>>> list(w)
[('.', ['project'], ['main.py']), ('./project', [], ['control.py', 'a.py', 'view.py', 'model.py'])]
>>> list(os.walk('..'))
[('..', ['codes', 'data', 'images'], ['README.md']), ('../codes', ['project'], ['main.py']), ('../codes/project', [], ['control.py', 'a.py', 'view.py', 'model.py']), ('../data', [], ['secrets.txt', 'python.txt']), ('../images', ['animal'], []), ('../images/animal', [], ['ant.jpg', 'dog.png', 'cat.jpg'])]
6 changes: 6 additions & 0 deletions Python/ch4.File Handling/Work with OS_6.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
>>> os.path.exists('project')
True
>>> os.path.exists('project/model.py')
True
>>> os.path.exists('sample.py')
False
7 changes: 7 additions & 0 deletions Python/ch4.File Handling/Work with OS_7.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# found to file or folder with isfile() and isdir()
>>> os.path.isdir('project')
True
>>> os.path.isdir('project/view.py')
False
>>> os.path.isfile('project/view.py')
True
10 changes: 10 additions & 0 deletions Python/ch4.File Handling/Work with OS_8.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
>>> os.sep
'/'
>>> '../main/images'.split(os.sep)
['..', 'main', 'images']
>>> os.path.basename('project/control.py')
'control.py'
>>> os.path.basename('../images/animal/cat.jpg')
'cat.jpg'
>>> os.path.basename('../main/images')
'images'
4 changes: 4 additions & 0 deletions Python/ch4.File Handling/Work with OS_9.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
>>> os.path.join('..', 'images', '2019')
'../images/2019'
>>> os.sep.join(['..', 'images', '2019'])
'../images/2019'

0 comments on commit 9d2a83d

Please sign in to comment.