-4

i have the next code:

    bestand = open("6_frame.txt", "r")
    seq = bestand.readlines()
    #to make a list
    for line in seq:
        alle = line
        while True: 
            if alle.isupper():
                break
            else:
                print ("try again ")

with this code i want to make sure that someone , who write a sequence in the file, write this sequence in capital letters, and want to except the other errors: but he want do what i want.

can somebody help me ??

Emine Poyraz
  • 37
  • 1
  • 8
  • 2
    German doesn't belong into sourcecode files (and this comment comes from a German). Besides that, your question is very unclear. Your loop will never exit if the line is not uppercase since the content of `alle` won't ever change. – ThiefMaster Jan 08 '12 at 14:35
  • i am just wondering why bother user to make all upper. Cant you just use `alle = alle.upper()` or alike? – yosukesabai Jan 08 '12 at 16:24
  • Emine, this is nonsense: you are asking to "try again" to a text file. – joaquin Jan 09 '12 at 21:33

3 Answers3

3

I think you are saying you want to ensure the entire file is in upper-case. If that's what you are looking for, this will do the trick:

if all(x.isupper() for x in open("6_frame.txt", "r")):
    print("entire file is upper-case.")
else:
    print("try again!")

This will test the file, line-at-a-time, for all upper-case characters. If it finds a line that is not, it will return false, otherwise if all lines are upper-case, return true (and print "entire file is upper-case").

Update (File watching edition)

It looks like you want to keep checking the file until it's all uppercase. Here's a fairly ineffecient way to do it (you could add modtime checks, or use inotify to make it better):

from time import sleep

while True:
    lines = open("6_frame.txt", "r")
    if all((x.isupper() or x.isspace()) for x in lines):
        print("entire file is upper-case.")
        break  # We're done watching file, exit loop
    else:
        print("try again!")

    sleep(1)   # Wait for user to correct file

Also, you may get exceptions (I'm not sure) if the person is mid-save when your script checks the file again, so you may need to add some exception catching around the all line. Either way... Hope this helps!

Adam Wagner
  • 15,469
  • 7
  • 52
  • 66
  • your code is the thing i mean , but when the code in the file is wrong, i got a lot of "try again!" and it doesn't start again ! – Emine Poyraz Jan 08 '12 at 15:05
  • @user1136962 I think I follow what you are looking for.. I've posted an update – Adam Wagner Jan 08 '12 at 15:50
  • `open()` yields lines *including* newline characters, but `"\n".isupper() is False` therefore your code doesn't work if there is a newline in the file. – jfs Jan 08 '12 at 16:50
  • @J.F.Sebastian, actually, it's only a problem if the newline is the *only* thing on the line. this line, for example, evaluates to true: `"FOO\n".isupper()`. I've updated my example to include lines that are *only* whitespace as well... obviously this could keep going though, what about lines with only punctuation? Either way, I think this is enough for the OP to see where to proceed from here. – Adam Wagner Jan 08 '12 at 18:50
  • You're right it fails only if the line doesn't contain cased characters, e.g., `"A,\n".isupper() is True` despite `",\n".isupper()` being `False`. Your code might produce different results compared to `open("6_frame.txt").read().isupper()`, but it is unclear what behavior the OP prefers. – jfs Jan 08 '12 at 19:58
0

My abc.txt content is aBC, so not all uppercase:

fd = open('abc.txt','r')
seq = fd.readlines()
for line in seq:
       if line.isupper():
                  print('all capital')
       else:
                print('try again')

therefore my output = try again

if my abc.txt content is ABC my output is all capital

RanRag
  • 48,359
  • 38
  • 114
  • 167
0

Determine whether all cased characters in the file are uppercase, retry if not:

import time
from hashlib import md5

hprev = None
while True:
    with open("6_frame.txt") as f:
         text = f.read()
         if text.isupper():
            print('all capital')
            break
         else:
            h = md5(text).hexdigest()
            if h != hprev: # print message if the file changed
               print('try again')
            hprev = h
            time.sleep(1) # wait for the file to change
jfs
  • 399,953
  • 195
  • 994
  • 1,670