Hướng dẫn what is file position in python? - vị trí tệp trong python là gì?
Sự mô tảPhương thức tệp Python Tell () Trả về vị trí hiện tại của tệp đọc/ghi tệp trong tệp.tell() returns the current position of the file read/write pointer within the file. Show Cú phápSau đây là Phương thức Syntax cho Tell () -tell() method − fileObject.tell() Thông số
Giá trị trả vềPhương thức này trả về vị trí hiện tại của tệp đọc/ghi tệp trong tệp. Thí dụVí dụ sau đây cho thấy cách sử dụng phương thức Tell (). This is 1st line This is 2nd line This is 3rd line This is 4th line This is 5th line #!/usr/bin/python # Open a file fo = open("foo.txt", "rw+") print "Name of the file: ", fo.name # Assuming file has following 5 lines # This is 1st line # This is 2nd line # This is 3rd line # This is 4th line # This is 5th line line = fo.readline() print "Read Line: %s" % (line) # Get the current position of the file. pos = fo.tell() print "Current Position: %d" % (pos) # Close opend file fo.close() Khi chúng tôi chạy trên chương trình, nó tạo ra kết quả sau - Name of the file: foo.txt Read Line: This is 1st line Current Position: 28 python_files_io.htm Con trỏ tệp ở cuối tệp nếu tệp tồn tại. Tệp mở trong chế độ phụ lục. Nếu tệp không tồn tại, nó sẽ tạo một tệp mới để đọc và ghi. Đối tượng tệp trong ví dụ Python là gì? Đối tượng tệp là gì? Đối tượng tệp Python cung cấp các phương thức và thuộc tính để truy cập và thao tác các tệp. Sử dụng các đối tượng tệp, chúng ta có thể đọc hoặc viết bất kỳ tệp nào. Bất cứ khi nào chúng tôi mở một tệp để thực hiện bất kỳ hoạt động nào trên đó, Python trả về một đối tượng tệp.
tìm kiếm () phương thứcTrong Python, hàm Seek () được sử dụng để thay đổi vị trí của xử lý tệp thành một vị trí cụ thể nhất định. Tệp xử lý giống như một con trỏ, xác định từ nơi dữ liệu phải được đọc hoặc ghi trong tệp. & Nbsp; & nbsp;change the position of the File Handle to a given specific position. File handle is like a cursor, which defines
from where the data has to be read or written in the file.
Điểm tham chiếu được chọn bởi đối số từ_what. Nó chấp nhận ba giá trị: & nbsp; & nbsp;from_what argument. It
accepts three values:
Theo mặc định, từ đối số được đặt thành 0.Note: điểm tham chiếu ở vị trí hiện tại / đầu của tệp không thể được đặt ở chế độ văn bản trừ khi độ lệch bằng 0. Ví dụ 1: giả sử chúng ta phải đọc một tệp có tên là GFG.TXT Có chứa văn bản sau: & nbsp; & nbsp; "Code is like humor. When you have to explain it, it’s bad." Python3This is 1st line This is 2nd line This is 3rd line This is 4th line This is 5th line1 This is 1st line This is 2nd line This is 3rd line This is 4th line This is 5th line2 This is 1st line This is 2nd line This is 3rd line This is 4th line This is 5th line3 This is 1st line This is 2nd line This is 3rd line This is 4th line This is 5th line4 This is 1st line This is 2nd line This is 3rd line This is 4th line This is 5th line5 This is 1st line This is 2nd line This is 3rd line This is 4th line This is 5th line6 This is 1st line This is 2nd line This is 3rd line This is 4th line This is 5th line7 This is 1st line This is 2nd line This is 3rd line This is 4th line This is 5th line8 This is 1st line This is 2nd line This is 3rd line This is 4th line This is 5th line9 #!/usr/bin/python # Open a file fo = open("foo.txt", "rw+") print "Name of the file: ", fo.name # Assuming file has following 5 lines # This is 1st line # This is 2nd line # This is 3rd line # This is 4th line # This is 5th line line = fo.readline() print "Read Line: %s" % (line) # Get the current position of the file. pos = fo.tell() print "Current Position: %d" % (pos) # Close opend file fo.close()0 This is 1st line This is 2nd line This is 3rd line This is 4th line This is 5th line8 #!/usr/bin/python # Open a file fo = open("foo.txt", "rw+") print "Name of the file: ", fo.name # Assuming file has following 5 lines # This is 1st line # This is 2nd line # This is 3rd line # This is 4th line # This is 5th line line = fo.readline() print "Read Line: %s" % (line) # Get the current position of the file. pos = fo.tell() print "Current Position: %d" % (pos) # Close opend file fo.close()2 #!/usr/bin/python # Open a file fo = open("foo.txt", "rw+") print "Name of the file: ", fo.name # Assuming file has following 5 lines # This is 1st line # This is 2nd line # This is 3rd line # This is 4th line # This is 5th line line = fo.readline() print "Read Line: %s" % (line) # Get the current position of the file. pos = fo.tell() print "Current Position: %d" % (pos) # Close opend file fo.close()3 #!/usr/bin/python # Open a file fo = open("foo.txt", "rw+") print "Name of the file: ", fo.name # Assuming file has following 5 lines # This is 1st line # This is 2nd line # This is 3rd line # This is 4th line # This is 5th line line = fo.readline() print "Read Line: %s" % (line) # Get the current position of the file. pos = fo.tell() print "Current Position: %d" % (pos) # Close opend file fo.close()2 #!/usr/bin/python # Open a file fo = open("foo.txt", "rw+") print "Name of the file: ", fo.name # Assuming file has following 5 lines # This is 1st line # This is 2nd line # This is 3rd line # This is 4th line # This is 5th line line = fo.readline() print "Read Line: %s" % (line) # Get the current position of the file. pos = fo.tell() print "Current Position: %d" % (pos) # Close opend file fo.close()5 #!/usr/bin/python # Open a file fo = open("foo.txt", "rw+") print "Name of the file: ", fo.name # Assuming file has following 5 lines # This is 1st line # This is 2nd line # This is 3rd line # This is 4th line # This is 5th line line = fo.readline() print "Read Line: %s" % (line) # Get the current position of the file. pos = fo.tell() print "Current Position: %d" % (pos) # Close opend file fo.close()6 Output: 20 When you have to explain it, it’s bad. Ví dụ 2: Hàm Seek () với phần bù âm chỉ hoạt động khi tệp được mở ở chế độ nhị phân. Hãy giả sử các tệp nhị phân chứa các văn bản sau. & NBSP;Seek() function with
negative offset only works when file is opened in binary mode. Let’s suppose the binary file contains the following text. b'Code is like humor. When you have to explain it, its bad.' Python3This is 1st line This is 2nd line This is 3rd line This is 4th line This is 5th line1 This is 1st line This is 2nd line This is 3rd line This is 4th line This is 5th line2 This is 1st line This is 2nd line This is 3rd line This is 4th line This is 5th line3 This is 1st line This is 2nd line This is 3rd line This is 4th line This is 5th line4 Name of the file: foo.txt Read Line: This is 1st line Current Position: 281 This is 1st line This is 2nd line This is 3rd line This is 4th line This is 5th line6 Name of the file: foo.txt Read Line: This is 1st line Current Position: 283 This is 1st line This is 2nd line This is 3rd line This is 4th line This is 5th line8 This is 1st line This is 2nd line This is 3rd line This is 4th line This is 5th line9 Name of the file: foo.txt Read Line: This is 1st line Current Position: 286 Name of the file: foo.txt Read Line: This is 1st line Current Position: 287 This is 1st line This is 2nd line This is 3rd line This is 4th line This is 5th line6 Name of the file: foo.txt Read Line: This is 1st line Current Position: 289 This is 1st line This is 2nd line This is 3rd line This is 4th line This is 5th line8 #!/usr/bin/python # Open a file fo = open("foo.txt", "rw+") print "Name of the file: ", fo.name # Assuming file has following 5 lines # This is 1st line # This is 2nd line # This is 3rd line # This is 4th line # This is 5th line line = fo.readline() print "Read Line: %s" % (line) # Get the current position of the file. pos = fo.tell() print "Current Position: %d" % (pos) # Close opend file fo.close()2 #!/usr/bin/python # Open a file fo = open("foo.txt", "rw+") print "Name of the file: ", fo.name # Assuming file has following 5 lines # This is 1st line # This is 2nd line # This is 3rd line # This is 4th line # This is 5th line line = fo.readline() print "Read Line: %s" % (line) # Get the current position of the file. pos = fo.tell() print "Current Position: %d" % (pos) # Close opend file fo.close()3 #!/usr/bin/python # Open a file fo = open("foo.txt", "rw+") print "Name of the file: ", fo.name # Assuming file has following 5 lines # This is 1st line # This is 2nd line # This is 3rd line # This is 4th line # This is 5th line line = fo.readline() print "Read Line: %s" % (line) # Get the current position of the file. pos = fo.tell() print "Current Position: %d" % (pos) # Close opend file fo.close()2 "Code is like humor. When you have to explain it, it’s bad."4 "Code is like humor. When you have to explain it, it’s bad."5 "Code is like humor. When you have to explain it, it’s bad."6 #!/usr/bin/python # Open a file fo = open("foo.txt", "rw+") print "Name of the file: ", fo.name # Assuming file has following 5 lines # This is 1st line # This is 2nd line # This is 3rd line # This is 4th line # This is 5th line line = fo.readline() print "Read Line: %s" % (line) # Get the current position of the file. pos = fo.tell() print "Current Position: %d" % (pos) # Close opend file fo.close()6
Có thêm hai phương thức đối tượng tệp được sử dụng để xác định hoặc nhận vị trí tệp.
tell(): Phương pháp này được sử dụng để cho chúng tôi biết vị trí hiện tại trong tệp có nghĩa là hoạt động đọc hoặc ghi tiếp theo sẽ được thực hiện rằng nhiều byte cách xa đầu tệp.syntax: obj.tell() Thí dụ : """ Author : ITVoyagers (https://itvoyagers.in/) Date :23rd December 2018 Description : Program to use of tell() """ #create file object and show the use tell() object=open("itvoyagers.txt",'w') object.write("first statement n") object.write("second statement n") object=open("itvoyagers.txt",'r') s=11 c=object.read(s) print(object.tell()) #tells the position based on parameter passed in read operation g=object.read() print(object.tell()) #tells position after performing read() on entire file seek(): Phương pháp này được sử dụng để thay đổi vị trí hiện tại của tệp. Phương thức này có hai tham số chính bù và từ.syntax: obj.seek(offset,from) Ở đây, đối số bù có nghĩa là số byte sẽ được di chuyển. Đối số từoffset argument means the number of bytes to be moved.
Thí dụ : This is 1st line This is 2nd line This is 3rd line This is 4th line This is 5th line0 Phương pháp này được sử dụng để thay đổi vị trí hiện tại của tệp. Phương thức này có hai tham số chính bù và từ.syntax:
Tệp tìm kiếm trong Python là gì?Phương thức Python Seek () Phương thức Seek () Đặt vị trí tệp hiện tại trong luồng tệp.Phương thức Seek () cũng trả về POSTION MỚI.sets the current file position in a file stream. The seek() method also returns the new postion.
Con trỏ tệp trong Python là gì?Con trỏ tệp ở cuối tệp nếu tệp tồn tại.Tệp mở trong chế độ phụ lục.Nếu tệp không tồn tại, nó sẽ tạo một tệp mới để đọc và ghi.at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.
Đối tượng tệp trong ví dụ Python là gì?Đối tượng tệp là gì?Đối tượng tệp Python cung cấp các phương thức và thuộc tính để truy cập và thao tác các tệp.Sử dụng các đối tượng tệp, chúng ta có thể đọc hoặc viết bất kỳ tệp nào.Bất cứ khi nào chúng tôi mở một tệp để thực hiện bất kỳ hoạt động nào trên đó, Python trả về một đối tượng tệp.provides methods and attributes to access and manipulate files. Using file objects, we can read or write any files. Whenever we open a file to perform any operations on it, Python returns a file object.
Việc sử dụng tệp trong Python là gì?Python có hàm Open () tích hợp để mở tệp.Hàm này trả về một đối tượng tệp, còn được gọi là tay cầm, vì nó được sử dụng để đọc hoặc sửa đổi tệp cho phù hợp.Chúng tôi có thể chỉ định chế độ trong khi mở một tệp.Trong chế độ, chúng tôi chỉ định xem chúng tôi muốn đọc r, viết W hay nối A vào tệp.to read or modify the file accordingly. We can specify the mode while opening a file. In mode, we specify whether we want to read r , write w or append a to the file. |