Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb19b464

Browse files
committed
add grep clone tutorial
1 parent07d02c3 commitb19b464

File tree

4 files changed

+36
-0
lines changed

4 files changed

+36
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
214214
-[How to Print Variable Name and Value in Python](https://www.thepythoncode.com/article/print-variable-name-and-value-in-python). ([code](python-standard-library/print-variable-name-and-value))
215215
-[How to Make a Hangman Game in Python](https://www.thepythoncode.com/article/make-a-hangman-game-in-python). ([code](python-standard-library/hangman-game))
216216
-[How to Use the Argparse Module in Python](https://www.thepythoncode.com/article/how-to-use-argparse-in-python). ([code](python-standard-library/argparse))
217+
-[How to Make a Grep Clone in Python](https://thepythoncode.com/article/how-to-make-grep-clone-in-python). ([code](python-standard-library/grep-clone))
217218

218219
-###[Using APIs](https://www.thepythoncode.com/topic/using-apis-in-python)
219220
-[How to Automate your VPS or Dedicated Server Management in Python](https://www.thepythoncode.com/article/automate-veesp-server-management-in-python). ([code](general/automating-server-management))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#[How to Make a Grep Clone in Python](https://thepythoncode.com/article/how-to-make-grep-clone-in-python)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Import the necessary libraries.
2+
importre,sys
3+
fromcoloramaimportinit,Fore
4+
5+
# Initialize colorama.
6+
init()
7+
8+
# Grep function.
9+
defgrep(pattern,filename):
10+
try:
11+
found_match=False
12+
withopen(filename,'r')asfile:
13+
forlineinfile:
14+
ifre.search(pattern,line):
15+
# Print matching lines in green.
16+
print(Fore.GREEN+line.strip()+"\n")# We are including new lines to enhance readability.
17+
found_match=True
18+
ifnotfound_match:
19+
# Print message in red if no content is found.
20+
print(Fore.RED+f"No content found matching the pattern '{pattern}'.")
21+
exceptFileNotFoundError:
22+
# Print error message in red if the file is not found.
23+
print(Fore.RED+f"File '{filename}' not found.")
24+
25+
26+
iflen(sys.argv)!=3:
27+
# Print usage message in red if the number of arguments is incorrect.
28+
print(Fore.RED+"Usage: python grep_python.py <pattern> <filename>")
29+
sys.exit(1)
30+
31+
pattern=sys.argv[1]
32+
filename=sys.argv[2]
33+
grep(pattern,filename)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
colorama

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp