Skip to content

Instantly share code, notes, and snippets.

@keithweaver
Last active October 8, 2021 11:56
Show Gist options
  • Save keithweaver/b0912519d410b7e2ab3c98bf350bcfc2 to your computer and use it in GitHub Desktop.
Save keithweaver/b0912519d410b7e2ab3c98bf350bcfc2 to your computer and use it in GitHub Desktop.
Get File Content with Python
# 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')
@kallewesterling
Copy link

two-line way of doing the same thing:

from pathlib import Path
fileContent = Path('./test.txt').read_text()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment