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.

Cú pháp

Sau đây là Phương thức Syntax cho Tell () -tell() method −

fileObject.tell()

Thông số

  • Na

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.
 

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.
     
  • Cải thiện bài viết
     

tìm kiếm () phương thức

Trong 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. 
 

Cú pháp: f.seek (offset, from_what), trong đó f là tập tin pulfparameter: & nbsp; offset: số lượng vị trí để di chuyển về phía trước & nbsp; from_what: nó xác định điểm tham chiếu. Returns: trả về vị trí tuyệt đối mới.f.seek(offset, from_what), where f is file pointer
Parameters: 
Offset: Number of positions to move forward 
from_what: It defines point of reference.
Returns: Return the new absolute position.

Đ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: 
 

  • 0: Đặt điểm tham chiếu ở đầu tệp & nbsp; & nbsp; sets the reference point at the beginning of the file 
     
  • 1: Đặt điểm tham chiếu tại vị trí tệp hiện tại & nbsp; & nbsp; sets the reference point at the current file position 
     
  • 2: Đặt điểm tham chiếu ở cuối tệp & nbsp; & nbsp; sets the reference point at the end of the file 
     

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;
Note: Reference point at current position / end of file cannot be set in text mode except when offset is equal to 0.
Example 1: Let’s suppose we have to read a file named “GfG.txt” which contains the following text: 
 

"Code is like humor. When you have to explain it, it’s bad."    

Python3

This is 1st line
This is 2nd line
This is 3rd line
This is 4th line
This is 5th line
1
This is 1st line
This is 2nd line
This is 3rd line
This is 4th line
This is 5th line
2
This is 1st line
This is 2nd line
This is 3rd line
This is 4th line
This is 5th line
3
This is 1st line
This is 2nd line
This is 3rd line
This is 4th line
This is 5th line
4
This is 1st line
This is 2nd line
This is 3rd line
This is 4th line
This is 5th line
5
This is 1st line
This is 2nd line
This is 3rd line
This is 4th line
This is 5th line
6
This is 1st line
This is 2nd line
This is 3rd line
This is 4th line
This is 5th line
7
This is 1st line
This is 2nd line
This is 3rd line
This is 4th line
This is 5th line
8

This is 1st line
This is 2nd line
This is 3rd line
This is 4th line
This is 5th line
9
#!/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 line
8

#!/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.'

Python3

This is 1st line
This is 2nd line
This is 3rd line
This is 4th line
This is 5th line
1
This is 1st line
This is 2nd line
This is 3rd line
This is 4th line
This is 5th line
2
This is 1st line
This is 2nd line
This is 3rd line
This is 4th line
This is 5th line
3
This is 1st line
This is 2nd line
This is 3rd line
This is 4th line
This is 5th line
4
Name of the file:  foo.txt
Read Line: This is 1st line

Current Position: 28
1
This is 1st line
This is 2nd line
This is 3rd line
This is 4th line
This is 5th line
6
Name of the file:  foo.txt
Read Line: This is 1st line

Current Position: 28
3
This is 1st line
This is 2nd line
This is 3rd line
This is 4th line
This is 5th line
8

This is 1st line
This is 2nd line
This is 3rd line
This is 4th line
This is 5th line
9
Name of the file:  foo.txt
Read Line: This is 1st line

Current Position: 28
6
Name of the file:  foo.txt
Read Line: This is 1st line

Current Position: 28
7
This is 1st line
This is 2nd line
This is 3rd line
This is 4th line
This is 5th line
6
Name of the file:  foo.txt
Read Line: This is 1st line

Current Position: 28
9
This is 1st line
This is 2nd line
This is 3rd line
This is 4th line
This is 5th line
8

#!/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


  • Vị trí tập tin
    • tell():
    • tìm kiếm():

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()
    seek()

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:
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:
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.
from argument is used to specify the reference position from where the bytes needs to be moved.
Points to Remember for “from” argument

    Nếu từ được đặt thành 0, nó có nghĩa là sử dụng đầu của tệp làm vị trí tham chiếu
    Nếu từ được đặt thành 1, nó có nghĩa là sử dụng vị trí hiện tại của tệp làm vị trí tham chiếu
    Nếu từ được đặt thành 2, nó có nghĩa là sử dụng phần cuối của tệp làm vị trí tham chiếu

Thí dụ :

This is 1st line
This is 2nd line
This is 3rd line
This is 4th line
This is 5th line
0

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:

  • Ở đây, đối số bù có nghĩa là số byte sẽ được di chuyển. Đối số từ
  • Nếu từ được đặt thành 0, nó có nghĩa là sử dụng đầu của tệp làm vị trí tham chiếu
  • Nếu từ được đặt thành 1, nó có nghĩa là sử dụng vị trí hiện tại của tệp làm vị trí tham chiếu
  • Nếu từ được đặt thành 2, nó có nghĩa là sử dụng phần cuối của tệp làm vị trí tham chiếu

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.