Re: File traversing
wow...thanks Karl :) On Tue, Apr 17, 2012 at 2:20 AM, Karl Knechtel zahl...@gmail.com wrote: On Sun, Apr 15, 2012 at 5:51 AM, Chris Angelico ros...@gmail.com wrote: (You may also want to consider using the 'with' statement to guarantee a timely closing of the file. Outside the scope of this mail though.) I think this list is just to collect unique entries, yes? If so, a set may be more to your liking. Check out: http://docs.python.org/py3k/library/stdtypes.html#set http://mail.python.org/mailman/listinfo/python-list In cases like this I like to just show the final code after making all the changes, and let the student ask questions :) with open('/bah') as res_own_file: all_res = set(line.strip().replace(' ', '').split(':')[1] for line in res_own_file if ':' in line) -- ~Zahlman {: -- http://mail.python.org/mailman/listinfo/python-list -- Regards Nibin. http://TechsWare.in -- http://mail.python.org/mailman/listinfo/python-list
Re: File traversing
# python test.py File test.py, line 1 with open('/etc/trueuserowners') as res_own_file: ^ IndentationError: unexpected indent [~]# cat test.py with open('/etc/trueuserowners') as res_own_file: all_res = set(line.strip().replace(' ', '').split(':')[1] for line in res_own_file if ':' in line) print all_res am I missing something? :) On Tue, Apr 17, 2012 at 4:44 PM, Nibin V M nibi...@gmail.com wrote: wow...thanks Karl :) On Tue, Apr 17, 2012 at 2:20 AM, Karl Knechtel zahl...@gmail.com wrote: On Sun, Apr 15, 2012 at 5:51 AM, Chris Angelico ros...@gmail.com wrote: (You may also want to consider using the 'with' statement to guarantee a timely closing of the file. Outside the scope of this mail though.) I think this list is just to collect unique entries, yes? If so, a set may be more to your liking. Check out: http://docs.python.org/py3k/library/stdtypes.html#set http://mail.python.org/mailman/listinfo/python-list In cases like this I like to just show the final code after making all the changes, and let the student ask questions :) with open('/bah') as res_own_file: all_res = set(line.strip().replace(' ', '').split(':')[1] for line in res_own_file if ':' in line) -- ~Zahlman {: -- http://mail.python.org/mailman/listinfo/python-list -- Regards Nibin. http://TechsWare.in -- Regards Nibin. http://TechsWare.in -- http://mail.python.org/mailman/listinfo/python-list
Re: File traversing
On Wed, Apr 18, 2012 at 12:31 AM, Nibin V M nibi...@gmail.com wrote: # python test.py File test.py, line 1 with open('/etc/trueuserowners') as res_own_file: ^ IndentationError: unexpected indent Make sure your first code line is flush left. Since indentation indicates block structure (and not scoping, as it does in C-like languages), Python refuses to allow it anywhere other than inside something that can make use of it. If you're copying and pasting a huge lot of code from somewhere and it's all indented a minimum of a couple of spaces, the simplest solution may be to add a line at the top saying if True:, thus making your whole program into a massive block if, which Python will happily accept. ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: File traversing
thanks for a super fast reply Chris :) On Tue, Apr 17, 2012 at 8:06 PM, Chris Angelico ros...@gmail.com wrote: On Wed, Apr 18, 2012 at 12:31 AM, Nibin V M nibi...@gmail.com wrote: # python test.py File test.py, line 1 with open('/etc/trueuserowners') as res_own_file: ^ IndentationError: unexpected indent Make sure your first code line is flush left. Since indentation indicates block structure (and not scoping, as it does in C-like languages), Python refuses to allow it anywhere other than inside something that can make use of it. If you're copying and pasting a huge lot of code from somewhere and it's all indented a minimum of a couple of spaces, the simplest solution may be to add a line at the top saying if True:, thus making your whole program into a massive block if, which Python will happily accept. ChrisA -- http://mail.python.org/mailman/listinfo/python-list -- Regards Nibin. http://TechsWare.in -- http://mail.python.org/mailman/listinfo/python-list
Re: File traversing
Thank you Chris :) ..I will check it... On Sun, Apr 15, 2012 at 3:21 PM, Chris Angelico ros...@gmail.com wrote: On Sun, Apr 15, 2012 at 7:15 PM, Nibin V M nibi...@gmail.com wrote: res_own_file = open('/bah') res_own_list = res_own_file.readline() res_tot_list=[] while res_own_list: res_own_list=res_own_list.strip() res_own_list=res_own_list.replace(' ', '') res_name=res_own_list.split(':') if res_name[1:2] not in res_tot_list: res_tot_list.append(res_name[1:2]) res_own_list = res_own_file.readline() You can iterate over the file thus: for res_own_list in res_own_file: # body of loop here That saves you the trouble of making sure you do the next readline at the bottom, too. (You may also want to consider using the 'with' statement to guarantee a timely closing of the file. Outside the scope of this mail though.) As you can see above, I am reading each line, cut a particular field and sort a unique list from it...I have two questions here 1. If I have to check the file again from beginning, I can't do it without closing and re-opening file since the file pointer is assigned to the EOF of the opened file right? If so, is there any alliterative method to do it? I don't wish to mess the script with multiple file open-close code! You should be able to use res_own_file.seek(0) to do that. I haven't tested your code, but that ought to work. If it fails, post code that uses seek and the error message you get, and someone will doubtless know what's wrong. 2. If I print res_name, it will display like ['one', 'two']. If I print res_name[0], it will display one ; but if I print res_name[1] it will display error out of index instead of two. From my code, when I use res_name[1:2] it's displaying second filed. Why it is behaving like this? My guess here is that at the end of the file, you get a blank line. When you use [1:2] syntax, you get back an empty list if the indices are out of bounds; but [1] will throw an error. if res_name[1:2] not in res_tot_list: res_tot_list.append(res_name[1:2]) I think this list is just to collect unique entries, yes? If so, a set may be more to your liking. Check out: http://docs.python.org/py3k/library/stdtypes.html#set Hope that's of some value! Chris Angelico -- http://mail.python.org/mailman/listinfo/python-list -- Regards Nibin. http://TechsWare.in -- http://mail.python.org/mailman/listinfo/python-list
Re: File traversing
On Sun, Apr 15, 2012 at 5:51 AM, Chris Angelico ros...@gmail.com wrote: (You may also want to consider using the 'with' statement to guarantee a timely closing of the file. Outside the scope of this mail though.) I think this list is just to collect unique entries, yes? If so, a set may be more to your liking. Check out: http://docs.python.org/py3k/library/stdtypes.html#set http://mail.python.org/mailman/listinfo/python-list In cases like this I like to just show the final code after making all the changes, and let the student ask questions :) with open('/bah') as res_own_file: all_res = set(line.strip().replace(' ', '').split(':')[1] for line in res_own_file if ':' in line) -- ~Zahlman {: -- http://mail.python.org/mailman/listinfo/python-list
File traversing
Hello, First of all..I am very new to python with no background in development area! :) Ok, here is my problem.I have opened a file and I need to check each line of that file. I have done it with a while loop. res_own_file = open('/bah') res_own_list = res_own_file.readline() res_tot_list=[] while res_own_list: res_own_list=res_own_list.strip() res_own_list=res_own_list.replace(' ', '') res_name=res_own_list.split(':') if res_name[1:2] not in res_tot_list: res_tot_list.append(res_name[1:2]) res_own_list = res_own_file.readline() As you can see above, I am reading each line, cut a particular field and sort a unique list from it...I have two questions here 1. If I have to check the file again from beginning, I can't do it without closing and re-opening file since the file pointer is assigned to the EOF of the opened file right? If so, is there any alliterative method to do it? I don't wish to mess the script with multiple file open-close code! 2. If I print res_name, it will display like ['one', 'two']. If I print res_name[0], it will display one ; but if I print res_name[1] it will display error out of index instead of two. From my code, when I use res_name[1:2] it's displaying second filed. Why it is behaving like this? Thanks in advance! Regards Nibin. http://TechsWare.in -- http://mail.python.org/mailman/listinfo/python-list
Re: File traversing
On Sun, Apr 15, 2012 at 7:15 PM, Nibin V M nibi...@gmail.com wrote: res_own_file = open('/bah') res_own_list = res_own_file.readline() res_tot_list=[] while res_own_list: res_own_list=res_own_list.strip() res_own_list=res_own_list.replace(' ', '') res_name=res_own_list.split(':') if res_name[1:2] not in res_tot_list: res_tot_list.append(res_name[1:2]) res_own_list = res_own_file.readline() You can iterate over the file thus: for res_own_list in res_own_file: # body of loop here That saves you the trouble of making sure you do the next readline at the bottom, too. (You may also want to consider using the 'with' statement to guarantee a timely closing of the file. Outside the scope of this mail though.) As you can see above, I am reading each line, cut a particular field and sort a unique list from it...I have two questions here 1. If I have to check the file again from beginning, I can't do it without closing and re-opening file since the file pointer is assigned to the EOF of the opened file right? If so, is there any alliterative method to do it? I don't wish to mess the script with multiple file open-close code! You should be able to use res_own_file.seek(0) to do that. I haven't tested your code, but that ought to work. If it fails, post code that uses seek and the error message you get, and someone will doubtless know what's wrong. 2. If I print res_name, it will display like ['one', 'two']. If I print res_name[0], it will display one ; but if I print res_name[1] it will display error out of index instead of two. From my code, when I use res_name[1:2] it's displaying second filed. Why it is behaving like this? My guess here is that at the end of the file, you get a blank line. When you use [1:2] syntax, you get back an empty list if the indices are out of bounds; but [1] will throw an error. if res_name[1:2] not in res_tot_list: res_tot_list.append(res_name[1:2]) I think this list is just to collect unique entries, yes? If so, a set may be more to your liking. Check out: http://docs.python.org/py3k/library/stdtypes.html#set Hope that's of some value! Chris Angelico -- http://mail.python.org/mailman/listinfo/python-list