-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0bc9b1e
commit 9d2a83d
Showing
11 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']) | ||
'' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
[] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'])] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |