Last active
October 8, 2021 11:56
-
-
Save keithweaver/b0912519d410b7e2ab3c98bf350bcfc2 to your computer and use it in GitHub Desktop.
Get File Content with Python
This file contains hidden or 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
# Example of getting file content | |
def getFileContent(pathAndFileName): | |
with open(pathAndFileName, 'r') as theFile: | |
# Return a list of lines (strings) | |
# data = theFile.read().split('\n') | |
# Return as string without line breaks | |
# data = theFile.read().replace('\n', '') | |
# Return as string | |
data = theFile.read() | |
return data | |
print getFileContent('./test.txt') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
two-line way of doing the same thing:
from pathlib import Path
fileContent = Path('./test.txt').read_text()