User Tools

Site Tools


python:file_handling

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
python:file_handling [2016/10/18 11:30] peterpython:file_handling [2020/07/15 09:30] (current) – external edit 127.0.0.1
Line 126: Line 126:
 new_path = '/home/john/new_days.txt' new_path = '/home/john/new_days.txt'
 new_days = open(new_path,'w') new_days = open(new_path,'w')
-Important to note, if new_days.txt already existed before opening the file its old contents would have been destroyed, so be careful when using the 'w' mode.+</file> 
 + 
 +Important to note, if new_days.txt already existed before opening the file its old contents would have been destroyed, so be careful when using the **'w'** mode.
  
 Once our new file is opened, we can put data into the file, using the write operation, **<file>.write()**.  The write operation takes a single parameter, which must be a string, and writes that string to the file.  If you want to start a new line in the file, you must explicitly provide the newline character.  First, we write the title to the file followed by the days of the week.  Let's also add in some print statements of what we are writing out, which is often good practice for tracking your scripts' progress. Once our new file is opened, we can put data into the file, using the write operation, **<file>.write()**.  The write operation takes a single parameter, which must be a string, and writes that string to the file.  If you want to start a new line in the file, you must explicitly provide the newline character.  First, we write the title to the file followed by the days of the week.  Let's also add in some print statements of what we are writing out, which is often good practice for tracking your scripts' progress.
Line 158: Line 160:
  
 <file python files.py> <file python files.py>
-path = '/users/sammy/days.txt'+path = '/home/john/days.txt'
 days_file = open(path,'r') days_file = open(path,'r')
 days = days_file.read() days = days_file.read()
  
- +new_path = '/home/john/new_days.txt'
-new_path = '/users/sammy/new_days.txt'+
 new_days = open(new_path,'w') new_days = open(new_path,'w')
  
python/file_handling.1476790259.txt.gz · Last modified: 2020/07/15 09:30 (external edit)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki