6 Replies - 3628 Views - Last Post: 04 May 2012 - 08:42 PM Rate Topic: -----

#1 crazy123  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 138
  • Joined: 04-August 11

File in Python

Posted 27 April 2012 - 09:46 AM

How I do this:

a = file.readline()
b = # save the position (line) read from the file
while a != "TEST":
   a = file.readline

c = # continue reading the file from the position b



??
Is This A Good Question/Topic? 0
  • +

Replies To: File in Python

#2 crazy123  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 138
  • Joined: 04-August 11

Re: File in Python

Posted 27 April 2012 - 07:08 PM

It's possible do this???
Was This Post Helpful? -1
  • +
  • -

#3 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: File in Python

Posted 28 April 2012 - 07:51 AM

Why don't you try and do it and find out? If you can't do it, tell us what you have tried and where you are having problems.
Was This Post Helpful? 2
  • +
  • -

#4 crazy123  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 138
  • Joined: 04-August 11

Re: File in Python

Posted 28 April 2012 - 07:58 AM

View PostSimown, on 28 April 2012 - 07:51 AM, said:

Why don't you try and do it and find out? If you can't do it, tell us what you have tried and where you are having problems.


The piece of code that I commented. I do not know how to do
Was This Post Helpful? 0
  • +
  • -

#5 Faitas  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 44
  • Joined: 04-November 11

Re: File in Python

Posted 28 April 2012 - 12:06 PM

If I didn't undurstand it correct, please correct me, don't be angry.

If you want to save each line to the variable, you can use a sequence :) Like this:
try:
    #First of all, let's open the file
    file = open('file_name.txt')

    #Create a sequence, where we can write down all line from the file
    each_lines = []

    #Iterate the file and write down each line
    for each_line in file:
        each_lines.append(each_line)

    #Now, you can do all you want to do with each line...
except IOError:
    print('This is a problem with your file')


I use exceptional handling because I want to get errors, which occurs in runtime... If it's didn't occur, the program will continue... You can use an alternative of try/except statment, OS standart library, like this:
import os

if os.path.exists('file_name.txt'):
    #First of all, let's open the file
    file = open('file_name.txt')

    #Create a sequence, where we can write down all line from the file
    each_lines = []

    #Iterate the file and write down each line
    for each_line in file:
        each_lines.append(each_line)

    #Now, you can do all you want to do with each line...
else:
    print('Your file isn\'t exist')

This post has been edited by Faitas: 28 April 2012 - 12:09 PM

Was This Post Helpful? 2
  • +
  • -

#6 crazy123  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 138
  • Joined: 04-August 11

Re: File in Python

Posted 28 April 2012 - 02:28 PM

View PostFaitas, on 28 April 2012 - 12:06 PM, said:

If I didn't undurstand it correct, please correct me, don't be angry.

If you want to save each line to the variable, you can use a sequence :) Like this:
try:
    #First of all, let's open the file
    file = open('file_name.txt')

    #Create a sequence, where we can write down all line from the file
    each_lines = []

    #Iterate the file and write down each line
    for each_line in file:
        each_lines.append(each_line)

    #Now, you can do all you want to do with each line...
except IOError:
    print('This is a problem with your file')


I use exceptional handling because I want to get errors, which occurs in runtime... If it's didn't occur, the program will continue... You can use an alternative of try/except statment, OS standart library, like this:
import os

if os.path.exists('file_name.txt'):
    #First of all, let's open the file
    file = open('file_name.txt')

    #Create a sequence, where we can write down all line from the file
    each_lines = []

    #Iterate the file and write down each line
    for each_line in file:
        each_lines.append(each_line)

    #Now, you can do all you want to do with each line...
else:
    print('Your file isn\'t exist')


Thank you
Was This Post Helpful? 0
  • +
  • -

#7 alpertt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 02-May 12

Re: File in Python

Posted 04 May 2012 - 08:42 PM

@Faitas, thanks, it helped me too :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1