Skip to content

Commit d2a37db

Browse files
committed
Update examples
1 parent c34cb29 commit d2a37db

File tree

15 files changed

+189
-0
lines changed

15 files changed

+189
-0
lines changed

command_line_args/01_argv.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"""
2+
Usage:
3+
python3 01_argv.py
4+
python3 01_argv.py a b c
5+
python3 01_argv.py "aaa aaad" abc
6+
"""
7+
import sys
8+
9+
print(sys.argv[0])
10+
print(len(sys.argv))
11+
for arg in sys.argv:
12+
print(arg)
13+

command_line_args/02_arguments.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"""
2+
python3 02_arguments.py
3+
python3 02_arguments.py --help
4+
python3 02_arguments.py --abc
5+
"""
6+
import argparse
7+
parser = argparse.ArgumentParser()
8+
parser.parse_args()

command_line_args/03_args.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""
2+
python3 03_args.py -h
3+
python3 03_args.py --dir /etc/
4+
python3 03_args.py -v --dir test_dir
5+
"""
6+
import argparse
7+
parser = argparse.ArgumentParser()
8+
parser.add_argument("--dir", help="Directory to list files")
9+
parser.add_argument("-v", "--verbosity", help="increase output verbosity",
10+
action="store_true")
11+
args = parser.parse_args()
12+
print(args.dir)
13+
print(args.verbosity)
14+

command_line_args/4913

Whitespace-only changes.

command_line_args/5036

Whitespace-only changes.

filesystem/01_stat.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""
2+
Usage examples:
3+
python3 ./01_stat.py ../read_from_stdin/
4+
python3 ./01_stat.py /dev/
5+
"""
6+
import os, sys
7+
from stat import *
8+
9+
def walktree(top, callback):
10+
'''recursively descend the directory tree rooted at top,
11+
calling the callback function for each regular file'''
12+
13+
for f in os.listdir(top):
14+
pathname = os.path.join(top, f)
15+
mode = os.lstat(pathname).st_mode
16+
if S_ISDIR(mode):
17+
# It's a directory, recurse into it
18+
walktree(pathname, callback)
19+
elif S_ISREG(mode):
20+
# It's a file, call the callback function
21+
callback(pathname)
22+
else:
23+
# Unknown file type, print a message
24+
print('Skipping %s' % pathname)
25+
26+
def visitfile(file):
27+
print('visiting', file)
28+
29+
if __name__ == '__main__':
30+
walktree(sys.argv[1], visitfile)

filesystem/02_globexample.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from pathlib import Path
2+
3+
tmp_dir = Path('/etc/')
4+
for fileobj in tmp_dir.glob('*.conf'):
5+
print(f'{fileobj.name} : ', end='')
6+
7+
with fileobj.open('r') as data_file:
8+
data = data_file.readline().rstrip()
9+
print(data)

filesystem/list_files_walk.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import os
2+
3+
directory = "/var"
4+
for root, dirs, files in os.walk("/var"):
5+
#for name in files:
6+
# print(os.path.join(root, name))
7+
for name in dirs:
8+
print(os.path.join(root, name))

filesystem/scandir.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import os
2+
3+
path = '/home/vagrant'
4+
with os.scandir(path) as it:
5+
for entry in it:
6+
if entry.name.startswith('.') and entry.is_file():
7+
print(entry.name)

read_from_stdin/01_sys_stdin.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"""
2+
Usage examples:
3+
echo test_string | python3 01_sys_stdin.py
4+
yes | head -10 | python3 01_sys_stdin.py
5+
python3 01_sys_stdin.py < 01_sys_stdin.py
6+
"""
7+
8+
import sys
9+
10+
11+
for line in sys.stdin:
12+
print(line.strip())
13+

0 commit comments

Comments
 (0)