hands

import os

if name == 'main':
'''
Python I/O
Python has serveral functions for createing,reading,updating,and deleting files.
functions:open,read,write,remove.

    key function open() takes two parameters:filename and mode,returns a file object.
    Operate modes:
        The "r" means to open a file for reading, default value. error if the file does not exist.
        The "a" means to open a file for appending. create the file if it not exist.
        The "w" means to open a file for writing,this will cover the exists data. create the file if not exist.
        The "x" means to create a new file in specified file name. error if the file exists.
    File mode:
        The "t" means to open a text file,default value.
        The "b" means to open a binary file.

    funtcion read() is one of the methods that the open() returns an object for reading the content of file.
        Default the read() will returns the whole text,but you can specify how many characters you want to return.

    function write() is another method  that the open() returns an object for writing text to the file.
'''
#open a file and print the content.
'''note: if the file not location at the project path, you need use the absolute path'''
f=open("../python_testfile/hello")
print(f.read())

#Read onlu parts of the file, you can use read() to specify the characters you want to read.
'''Note:using the read() func to read a file the index will location at the last of the file, there are no more data to read.
   if you read the file after read() func you will get an error,So if you want to read the the again, you need open it again.
   For testing this, we can use the read(int n) func to read the file and then use the read() func to read the rest of data of the file. 
   Please note the index changes.
'''
print("--------------Reading test----------------")
#Before the file had been read so we need open the file again.
f = open("../python_testfile/hello")
#First we read 5 characters using read(5).
print("First, we just read 5 characters:\n"+f.read(5))
#Then we use the read func to read the file,we will see the data after 5 characters. if you use the read() to read the f again, you will get nothoing.
print("Second, we read the rest of the file:\n"+f.read())

print("------------------------------")
#if you want to read a line of the file, you can use the readline() func to read the file.
'''if you want read the two first lines, use the realines() twice.
   Or you can use the readlines(int n) to specify the lines you wan to read.
   But you need to note the result readlines() returns is a iterator.
   if you want to get each of them,use the for loop to get it.
'''
#Use the readline() to read a line of the file.
f = open("../python_testfile/hello")
print("The first line is: "+f.readline())
print("The second line is: "+f.readline())

print("------------------------------")
#Use the readlines() to read some lines of the file.
f = open("../python_testfile/hello")
#This is a iterator
lines = f.readlines()
print(lines)

print("------------------------------")
#You can use loop through of the lines of the file also, you can read the whole file line by line;
f = open("../python_testfile/hello")
for x in f:
    print(x)


print("-------------Writing test-----------------")
#write someting to an exsiting file.
'''
To write to an existing file, you must add a parameter to the open() function,there are 2 another parameter that control the way writing data to a file.
Then you can use the write function to write what you want to the file.
Write mode:
    The 'a' means append data to the file.
    The 'w' means overwrite any exist content of the file.
'''
#We open the file and watch the existing content first.
f = open("../python_testfile/hello_cp","r")
print(f.read())

#Write something to the file using parameter "a".
f = open("../python_testfile/hello_cp","a")
f.write("this is what we add to the file,use append parameter")
f.close()

#Open the file to check.
f = open("../python_testfile/hello_cp")
print(f.read())

#This time we use the "w" parameter to write.
f = open("../python_testfile/hello_cp","w")
f.write("This time we use the w as parameter to open a file and writing data.")
f.close()

print("--------------------------------")
f = open("../python_testfile/hello_cp","r")
print(f.read())


print("-------------Creating test-----------------")
'''
The open function's three parameter could create a new file.
    The "x" will create a new file, return error if the file exists.
    The "a" will create a new file, if the file not exists.
    The "w" will create a new file, if the file not exists.
'''
f = open("../python_testfile/myfile.txt", "w")
f.close()

print("-------------Deleting test-----------------")
'''
To delete a file, you must import the OS module, and run its os.remove() function.
'''
#remove the file
os.remove("../python_testfile/myfile.txt")

#for avoid getting an error,you might want to check if the file exists before you try to delete it.
if os.path.exists("myfile.txt"):
    os.remove("myfile.txt")
else:
    print("The file does not exist.")

#remove a folder, you can use the os.rmdir() method, but just remove the empyt folder.
#os.rmdir("myfolder")

'''
you should always close the file after you are done with this.
In some cases,due to buffering, changes made to a file may not show until you close the file. 
'''
f.close()
posted @ 2022-10-10 20:37  yuexiuping  阅读(24)  评论(0编辑  收藏  举报