1 / 20

Python – reading and writing files

Python – reading and writing files. ???. ???two ways to open a file open and file ??? How to write to relative and absolute paths?. To Write a file. #written to C:Users jrwworkspacePython1 file = open( &quot;newfile.txt&quot; , &quot;w&quot; ) file.write ( &quot;Hello World <br>&quot; ) file.close ().

asher-lyons
Download Presentation

Python – reading and writing files

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Python – reading and writing files

  2. ??? • ???two ways to open a file open and file • ??? How to write to relative and absolute paths?

  3. To Write a file • #written to C:\Users\jrw\workspace\Python1 • file = open("newfile.txt", "w") • file.write("Hello World \n") • file.close()

  4. #written to C:\Users\jrw\workspace\Python1 • file = open("newfile2.txt", "w") • file.write("Hello") • file.write("World") • file.close() • #writes HelloWorld • Note: you need to include “\n” for newline.

  5. “””triple quotes””” • file = open("newfile3.txt", "w") • file.write(""" • Hello • Again""") • file.close() • #will write the following • Hello • Again

  6. What does this write? • #written to C:\Users\jrw\workspace\Python1 • file = open("newfile4.txt", "w") • file.write("Hello") • file.close() • file = open("newfile4.txt", "w") • file.write("Again") • file.close()

  7. What does this write? • #written to C:\Users\jrw\workspace\Python1 • file = open("newfile5.txt", "w") • file.write("Hello") • file.write("Again") • file.close()

  8. Printing to a file • file = open("newfile5.txt", "w") • print >> file, "START OF FILE" • print >> file, "END OF FILE" • del file • #this will print directly to a file • #del file deletes the file object and allows contents to be written to file.

  9. Appending a File • with open("test.txt", "a") asmyfile: • myfile.write("appended text")

  10. Reading a line of a File • input1 = file("newfile5.txt", "r") • printinput1.readline() • printinput1.readline() • #this will also read the “\n” at the end of line

  11. Read a file • input1 = file("newfile5.txt", "r") • printinput1.read() • #this will read the remainder of a file

  12. Reads a file into a list • with open("newfile5.txt") as f: • contents = f.readlines() • for content in contents: • print content • ??? Read a file into an array. • ??? Number of lines in a file

  13. Iterate through a file (line by line) • input1 = file("newfile5.txt", "r") • for line ininput1.readlines(): • printstr(len(line)) + " " + line • #adds the length of a line to the start of each line

  14. File Exceptions • A file or directory (path) does not exist. • You do not have read/write permissions • Disk error • try: • fh = open("output/testfile1.txt", "w") • fh.write("This is my test file for exception handling!!") • exceptIOError: • print"Error: can\'t find file or read data" • else: • print"Written content in the file successfully" • fh.close()

  15. Enter A Number • whileTrue: • try: • x = int(raw_input("Please enter a number: ")) • break • exceptValueError: • print"Oops! That was no valid number. Try again" • print"you entered" + str(x)

  16. Path - join • importos.path • filePath = os.path.join("output", "file1.txt") • #output\file1.txt • fh = open(filePath , "w") • fh.write("in file output\file1.txt on Windows machine")

  17. Path - split • importos.path • filePath = os.path.split("output/file1.txt") • (pathName, fileName) = filePath • printpathName# output • printfileName# file1.txt

  18. Path – split recursively • importos.path • defsplit_fully(path): • parent_path, name = os.path.split(path) • if name == "": • return (parent_path, ) • else: • returnsplit_fully(parent_path) + (name, ) • split_fully("dir1/dir2/file1.txt") • #('', 'dir1', 'dir2', 'file1.txt')

  19. Splitting file extensions • importos.path • printos.path.splitext("file.txt") • #('file', '.txt')

  20. If a path exists • printos.path.exists("C:\Users\jrw\file.txt") • #returns true or false

More Related