archivo Python seek () Método : Aquí, vamos a aprender sobre el método seek (), cómo establecer la posición actual del fichero en el lenguaje de programación Python?
de búsqueda de archivo () Método
seek () método es un método incorporado en Python, que se utiliza para establecer la posición actual del fichero (o puntero de archivo).
Sintaxis:
file_object.seek(offset)
Parámetro (s):
- compensado – Se especifica el desplazamiento a los conjuntos de la ubicación del archivo actual.
class valor:
El tipo class de este método es & lt; Return ‘return’ & gt; , devuelve la nueva posición del archivo.
Ejemplo:
# Python File seek() Method with Example
# creating a file
myfile = open("hello.txt", "w")
# writing to the file
res = myfile.write("Hello friends, how are you?")
print(res, "bytes written to the file.")
# closing the file
myfile.close()
# reading content from the file
myfile = open("hello.txt", "r")
print("file content...")
print(myfile.read())
# sets the current file location to 6
print("file content from 6th position...")
myfile.seek(6)
print(myfile.read())
# sets the current file location to 0
print("file content from 0th position...")
myfile.seek(0)
print(myfile.read())
# sets the current file location to 12
print("file content from 12th position...")
myfile.seek(12)
print(myfile.read())
salida
27 bytes written to the file.
file content...
Hello friends, how are you?
file content from 6th position...friends, how are you?
file content from 0th position...
Hello friends, how are you?
file content from 12th position...
s, how are you?