Jump to content

Files: Difference between revisions

From Slow Like Wiki
Rob (talk | contribs)
No edit summary
 
(5 intermediate revisions by one other user not shown)
Line 5: Line 5:
from pathlib import Path
from pathlib import Path


path = Path('pi_digits.txt') # locate file (in current directory)
path = Path('gravitys_rainbow.txt')     # specify path to file (in current directory)
contents = path.read_text().rstrip() # read its contents into memory as a string, removing any whitespace
try:                                    # begin error handling
print(f'File as one string:\n{contents}')
    contents = path.read_text().rstrip() # read contents into a string, removing any whitespace


lines = contents.splitlines() # separate file by line breaks
    print(f'File as one string:\n{contents}')
print('File as one string per line:')
 
for line in lines:
    lines = contents.splitlines()       # separate file by line breaks
     print(line)
    print('File as one string per line:')
    for line in lines:
        print(line)
except FileNotFoundError:                # If file not found
     print(f"Sorry, the file {path} does not exist.")
</syntaxhighlight>
</syntaxhighlight>


== Write to a File ==
== Write to a File ==
<syntaxhighlight lang="python" line>
<syntaxhighlight lang="python" line>
from pathlib import Path


gr = "A screaming comes across the sky. "
gr += "It has happened before, but there is nothing to compare it to now. "
gr += "It is too late. The evacuation still proceeds, but it's all theatre."
gr_file = Path('gravitys_rainbow.txt') # creates file if it doesn't exist
gr_file.write_text(gr)
</syntaxhighlight>
</syntaxhighlight>
== Reading and Writing JSON ==
<syntaxhighlight lang="python" line>
<syntaxhighlight lang="python" line>
from pathLib import Path
import json
numbers = [2, 3, 5, 7, 11, 13]
path = Path('numbers.json')
contents = json.dumps(numbers)      # convert the numbers list to json format
path.write_text(contents)
path = Path('numbers.json')
contents = Path.read_text()
read_numbers = json.loads(contents)
print(numbers)


</syntaxhighlight>
</syntaxhighlight>
Line 31: Line 56:


</syntaxhighlight>
</syntaxhighlight>
[[Category:Python]]

Latest revision as of 17:39, 16 February 2025

Note: VC Code does not, by default, run python files in the directory where they are saved. To change this behavior, click Code > Settings > Settings, search for "python.terminal.executeInFileDir", and enable it.

Read from a File


from pathlib import Path

path = Path('gravitys_rainbow.txt')      # specify path to file (in current directory)
try:                                     # begin error handling
    contents = path.read_text().rstrip() # read contents into a string, removing any whitespace

    print(f'File as one string:\n{contents}')

    lines = contents.splitlines()        # separate file by line breaks
    print('File as one string per line:')
    for line in lines:
        print(line)
except FileNotFoundError:                # If file not found
    print(f"Sorry, the file {path} does not exist.")

Write to a File


from pathlib import Path

gr = "A screaming comes across the sky. "
gr += "It has happened before, but there is nothing to compare it to now. "
gr += "It is too late. The evacuation still proceeds, but it's all theatre."
gr_file = Path('gravitys_rainbow.txt') # creates file if it doesn't exist
gr_file.write_text(gr)

Reading and Writing JSON


from pathLib import Path
import json

numbers = [2, 3, 5, 7, 11, 13]

path = Path('numbers.json')
contents = json.dumps(numbers)       # convert the numbers list to json format
path.write_text(contents)

path = Path('numbers.json')
contents = Path.read_text()
read_numbers = json.loads(contents)

print(numbers)