Python review -- about file

1 I don’t know why I can’t open a file with absolute path ,but a elative one works, my net has some troubles and I can’t Baidu it , I will save it later.

> >>> f = open('C:\Users\Chen\AppData\Local\Programs\Python\python.txt') 
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

2 Before you read a file with file.read() , make sure the position is where you want , sometimes when the pointer point the end ,you read nothing.

>>> f.read(5)
''
>>> f.seek(0,0)
0
>>> f.read(5)
'+++++'
>>> f.tell()
5
>>> f.close("snow.txt")

3 For python ,you don’t have to close a file in the end , python applies extrodinary garbage collection mechanism . But you should do it always because you may make mistakes in C with the bad habit.

4 file.read() will show you the newline-character ,but print(file.read()) won’t.

>>> f.read()
"+++++++++++\nPython News\n+++++++++++\n\nWhat's New in Python 3.6.4 final?\n=================================\n\n*Release date: 2017-12-18*\n\nThere were no new code changes in version 3.6.4 since v3.6.4rc1.\n\n


>>> print(f.read())
+++++++++++
Python News
+++++++++++

What's New in Python 3.6.4 final?
=================================

*Release date: 2017-12-18*

There were no new code changes in version 3.6.4 since v3.6.4rc1.

5 You can always go back to the beginning with file.seek(0,0)

6 It’s amazing that you can put a **.txt file into a list . On the contrary , you can put a list into a txt file , too .what’s amazing more , you can find a char or string or someting else in every line of the txt. That means you can judge whether a String is in a list or not.

>>> a = ["fuck off !","Come Back !","Go away !"]
>>> myFile_name = 'chen'
>>> myFile = open(myFile_name,'+w')
>>> myFile.write(a)
Traceback (most recent call last):
  File "<pyshell#163>", line 1, in <module>
    myFile.write(a)
TypeError: write() argument must be str, not list
>>> myFile.writelines(a)

>>> f.readline()
'+++++++++++\n'
>>> f.readline(5)
'Pytho'
>>> f.readline(-1)
'n News\n'
>>> list(f)
['+++++++++++\n', '\n', "What's New in Python 3.6.4 final?\n", '=================================\n'

>>> a = ["fuck off !","Come Back !","Go away !"]
>>> "fu" in a
False
>>> 'fuck' in a
False
>>> for each_line in myFile:
    print("fuck" in each_line)


True

7 If you want to creat a new file through function open(path,mode) , you can’t put a string into open(path,mode ) , like f = open(“chen.txt”,’+a’). Wrong . You need to define a String variable and pass it to this function.

>>> myFile = open("boy_*.txt",'+a')
Traceback (most recent call last):
  File "<pyshell#87>", line 1, in <module>
    myFile = open("boy_*.txt",'+a')
OSError: [Errno 22] Invalid argument: 'boy_*.txt'
>>> myFile = open("boy_*.txt")
Traceback (most recent call last):
  File "<pyshell#89>", line 1, in <module>
    myFile = open("boy_*.txt")
OSError: [Errno 22] Invalid argument: 'boy_*.txt'

>>> myFile_name = 'chen'
>>> myFile = open(myFile_name,'+w')
>>> myFile.write(a)
Traceback (most recent call last):
  File "<pyshell#163>", line 1, in <module>
    myFile.write(a)
TypeError: write() argument must be str, not list
>>> myFile.writelines(a)
>>> myFile
<_io.TextIOWrapper name='chen' mode='+w' encoding='cp936'>

8

>>> f = open("snow.txt",'+a')
>>> f.read()
''
>>> f.seek(0,0)
0
>>> f.read()
'\n fuck off !\n Come Back !'
>>> f.write('\nGo away !')
10
>>> f.read()
''
>>> f.seek(0,0)
0
>>> for each_line in f:
    print(each_line)




 fuck off !

 Come Back !

 Go away !

猜你喜欢

转载自blog.csdn.net/qq_36880027/article/details/78969713