I am working on a Python script ( 2.7 ) that will take a line of text from a text file and use it as a parameter for a function but i can not seem to figure out how to pass the line as a parameter for the function I also was wondering how would i modify a line that already exists in a file without opening it using a text editor within Python
#!/usr/bin/python
data = open("/tmp/INFO.txt", 'r')
Line = data.readlines()[2]
print Line
To get the specific line:
f = open("yourFile.txt", "r")
line = f.readlines()[yourLineNumber]
f.close()
To pass it as a parameter:
yourFunction(line)
To read a specific line and use it as a parameter, you first need to open the file then read the contents.
with open(filePath, 'r') as fd:
fileContents = fd.readlines()
Then you can can call fileContents[n]
where n is the index of the line you need.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.