iT邦幫忙

0

為何pyphon打開w然後立刻關上就會被刪掉?(字面翻,描述也看不太懂)

When a file is opened in write mode, the file's
existing content is deleted

file - open( "newfile.txt", "r")
print("Reading initial contents")
print(file.read())
print("Finished")
file.close()

file - open ( "newfile.txt", "W")
file.write("Some new text")
file.close()

file open ("newfile.txt", "r")
print("Reading new contents")
print(file.read))
print("Finished")
file.close()

給你一點建議,檔名可以換不同的,這樣比較好觀察. 加油.
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

5
froce
iT邦大師 1 級 ‧ 2018-08-20 21:12:04

open後面的參數,其中mode部分有分"r"、"w"、"a",然後還有一個"b"和"+"。
r代表讀。
w代表寫入並清空。
a代表增加。
r+代表可以讀寫。
w+會自動新增這檔案,並且清空檔案再寫入
a+會自動新增這檔案,如果有就會在檔案後增加。
有b則代表以二進位讀取/寫入。

https://stackoverflow.com/questions/1466000/python-open-built-in-function-difference-between-modes-a-a-w-w-and-r
可以參照這張圖

0
海綿寶寶
iT邦大神 1 級 ‧ 2018-08-21 10:13:37

這句話前面還有這麼長一大段說明
你先耐心看懂再說吧...

You can use Python to read and write the contents of files.
Text files are the easiest to manipulate. Before a file can be edited, it must be opened, using the open function.

    myfile = open(“filename.txt”)

The argument of the open function is the path to the file. If the file is in the same directory as the program, you can specify only its name.

You can specify the mode used to open a file by applying a second argument to the open function.
Sending “r” means open in read mode, which is the default. 
Sending “w” means write mode, for rewriting the contents of a file.
Sending “a” means append mode, for adding new content to the end of the file.
 
Adding “b” to a mode opens it in binary mode, which is used for non-text files (such as image and sound files).
For example:

    # write mode
     open(“filename.txt”, “w”)
     
     # read mode
     open(“filename.txt”, “r”)
     open(“filename.txt”)
     
     # binary write mode
     open(“filename.txt”, “wb”)

Once a file has been opened and used, you should close it.
This is done with the close method of the file object.

    file = open(“filename.txt”, “w”)
    # do stuff to the file
    file.close()

The contents of a file that has been opened in text mode can be read using the read method.

    file = open(“filename.txt”, “r”)
    cont = file.read()
    print(cont)
    file.close()

This will print all of the contents of the file “filename.txt”.

To read only a certain amount of a file, you can provide a number as an argument to the read function. This determines the number of bytes that should be read. 
You can make more calls to read on the same file object to read more of the file byte by byte. With no argument, read returns the rest of the file.

    file = open(“filename.txt”, “r”)
    print(file.read(16))
    print(file.read(4))
    print(file.read(4))
    print(file.read())
    file.close()

After all contents in a file have been read, any attempts to read further from that file will return an empty string, because you are trying to read from the end of the file.

    file = open(“filename.txt”, “r”)
    file.read()
    print(“Re-reading”)
    print(file.read())
    print(“Finished”)
    file.close()

    >>>Re-reading
    Finished

To retrieve each line in a file, you can use the readlines method to return a list in which each element is a line in the file.
For example:

    file = open(“filename.txt”, “r”)
    print(file.readlines())
    file.close()
    Result:

    >>>[‘Line 1 text \n’, ‘Line 2 text \n’, ‘Line 3 text’]

You can also use a for loop to iterate through the lines in the file:

    file = open(“filename.txt”, “r”)
    for line in file:
    print(line)
    file.close() 
    >>>
    Line 1 text
    Line 2 text
    Line 3 text

In the output, the lines are separated by blank lines, as the print function automatically adds a new line at the end of its output.

To write to files you use the write method, which writes a string to the file.
For example:

    file = open(“newfile.txt”, “w”)
    file.write(“This has been written to a file”)
    file.close()
     
    file = open(“newfile.txt”, “r”)
    print(file.read())
    file.close()

    >>>This has been written to a file

The “w” mode will create a file, if it does not already exist.

When a file is opened in write mode, the file’s existing content is deleted.

資料來源

我要發表回答

立即登入回答