Skip to content

File handling in Python #12068

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
4 changes: 4 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,9 @@
* [Resonant Frequency](electronics/resonant_frequency.py)
* [Wheatstone Bridge](electronics/wheatstone_bridge.py)

## File Handling
* [Read File](file_handling/read_file.py)

## File Transfer
* [Receive File](file_transfer/receive_file.py)
* [Send File](file_transfer/send_file.py)
Expand Down Expand Up @@ -1314,6 +1317,7 @@
* [Prefix Function](strings/prefix_function.py)
* [Rabin Karp](strings/rabin_karp.py)
* [Remove Duplicate](strings/remove_duplicate.py)
* [Replace](strings/replace.py)
* [Reverse Letters](strings/reverse_letters.py)
* [Reverse Words](strings/reverse_words.py)
* [Snake Case To Camel Pascal Case](strings/snake_case_to_camel_pascal_case.py)
Expand Down
18 changes: 18 additions & 0 deletions file_handling/read_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Relative path

Check failure on line 1 in file_handling/read_file.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (INP001)

file_handling/read_file.py:1:1: INP001 File `file_handling/read_file.py` is part of an implicit namespace package. Add an `__init__.py`.
def read_file():
fp = open(r"test_file.txt", "r")

Check failure on line 3 in file_handling/read_file.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP015)

file_handling/read_file.py:3:10: UP015 Unnecessary open mode parameters

Check failure on line 3 in file_handling/read_file.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (SIM115)

file_handling/read_file.py:3:10: SIM115 Use a context manager for opening files
# read file
text = fp.read()
# Closing the file after reading
fp.close()
"""
>>> read_file()
>>> print("Hello World!")
"""
return text


if __name__ == __main__:

Check failure on line 15 in file_handling/read_file.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F821)

file_handling/read_file.py:15:16: F821 Undefined name `__main__`
from doctest import testmod

testmod()
1 change: 1 addition & 0 deletions file_handling/test_file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello World!
25 changes: 25 additions & 0 deletions strings/replace.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
def string_replace(
text: str, input_string: str, replace_with_string: str, occurrence: int
) -> str:
"""
https://docs.python.org/3/library/stdtypes.html#str.replace
The replace() method replaces a specified string with another specified string.
The occurrence parameter can be skipped in order to consider all text.
Note: input and replace_with strings are case-sensitive.
>>> text = "One Two Two Three Four Five"
>>> string_val = string_replace(text, "Two", "Seven", 1)
>>> print(string_val)
One Seven Two Three Four Five

>>> text = "In the morning, the cat is running behind the mouse."
>>> string_val = string_replace(text, "the", "a", 10)
>>> print(string_val)
In a morning, a cat is running behind a mouse.
"""
return text.replace(input_string, replace_with_string, occurrence)


if __name__ == "__main__":
from doctest import testmod

testmod()
Loading