2014-01-05 63 views
0

正確輸入反正,我一直在試圖讀取和寫入在Python 3.3.3一個文本文件,它一直沒有工作。我的代碼如下:.txt文件無法讀取,並在Python 3.3.3

import math 
pFile=open('/Users/Username/Desktop/Programming:Design/Program Access Files/primes.txt') 
pList=[] 
for line in pFile: 
    pList=pList+(int(line.strip())) 
def testPrime(num,pList): 
    if num<=1: 
     return False 
    testList=[] 
    place=0 
    sqrt=math.sqrt(num)-((math.sqrt(num))%1) 
    p=pList[place] 
    while p<=sqrt: 
     testList.append(p) 
     place=place+1 
     p=pList[place] 
    for i in testList: 
     if i%num==0: 
      return False 
    return True 
print('Hello and Welcome to the Prime Finder 000!') 
end=int(input('What integer would you like me to go to today?')) 
for j in range(pList[-1],end+1): 
    if testPrime(j,pList)==True: 
     pList.append(j) 
     print(j) 
pFile.close() 
pFile=open('/Users/Username/Desktop/Programming:Design/Program Access Files/primes.txt','w') 
for k in pList: 
    pFile.write(str(k)) 
    pFile.write('\r\n') 
pFile.close() 

該程序應該搜索所有正整數來查找素數。我試圖存儲的文本文件在我嘗試打開它時顯示的目錄中找到了「primes.txt」。然而,有些東西一定是錯了我的閱讀文件,即這種方法:

pFile=open('/Users/Username/Desktop/Programming:Design/Program Access Files/primes.txt') 
    pList=[] 
    for line in pFile: 
     pList=pList+(int(line.strip())) 

我確信,我的尋找素數函數的工作,但他們沒有正確地存儲。目前,什麼程序做的是抹文本文件「primes.txt」,並從2打印出每個號碼由用戶輸入作爲主要的數量,在我還沒有找到一個訂單。

+0

因爲您使用相同的文件,因此您的文件被擦除,當您使用'w'而不是'a'寫入新文件時。這是你想要的嗎? – maurelio79

+0

我認爲它涵蓋了一點...但主要是我正在尋找一個原因,爲什麼我的閱讀'primes.txt'的方法不起作用。不過謝謝! – PythonDabbler

+0

我看不到代碼甚至可以運行。你正在向列表中添加一個int - 你不應該得到一個'TypeError'嗎? – DSM

回答

1

雅,如@ maurelio79和@DSM說,看起來像你讀取和寫在這裏,以相同的文件,你要添加一個int列出...這應該是不可能的。此外,用打開文件是清潔:

pList = [] 
with open(fle_path, 'r') as fle: 
    for line in fle: 
     pList.append(int(line.rstrip("\n"))) 


#find and append new primes to pList using pList.append(<new prime>) 


with open(fle_path, 'a') as fle: 
    for item in pList: 
     fle.write(str(item)+"\n") 

使用「R」來讀取文件,「W」與每次一個空白文件,用「a」到附加到現有文件啓動。您可以使用相同的文件,但使用'a'參數來附加新發現的素數。

與語句中使用時,它存在於循環自動關閉文件。

0

我有(大致)重寫你的例子告訴你什麼是被認爲是更實用。

首先,你的主要取景器可能是巨大的由我用一個基於測試只是一個單一的數字:

def isprime(n): 
    '''check if integer n is a prime''' 
    # make sure n is a positive integer 
    n = abs(int(n)) 
    # 0 and 1 are not primes 
    if n < 2: 
     return False 
    # 2 is the only even prime number 
    if n == 2: 
     return True  
    # all other even numbers are not primes 
    if not n & 1: 
     return False 
    # range starts with 3 and only needs to go up the squareroot of n 
    # for all odd numbers 
    for x in range(3, int(n**0.5)+1, 2): 
     if n % x == 0: 
      return False 
    return True 

接下來,我不知道什麼是你的源文件。在這裏,我只是把你的同一個問題「有多遠」,然後顯示什麼是更好的方式來讀取和寫入文件:

print('Hello and Welcome to the Prime Finder 000!') 
end=int(input('What integer would you like me to go to today?')) 

with open('/tmp/nums.txt', 'w') as fout: 
    for n in range(1,end+1): 
     fout.write('{}\n'.format(n)) 

最後,讀取文件中的整數2-end和測試每個依次對於素性。寫撇到一個新文件prime.txt

with open('/tmp/nums.txt') as f, open('/tmp/primes.txt', 'w') as fout: 
    for n in f: 
     n=int(n) 
     if isprime(n): 
      print(n) 
      fout.write('{}\n'.format(n)) 

如果有質數列表,這裏是你如何添加到列表中:

primes=[]   
with open('/tmp/nums.txt') as f: 
    for n in f: 
     n=int(n) 
     if isprime(n): 
      print(n) 
      primes.append(n) 

有幾件事情需要注意:

  1. 使用with關鍵字來打開一個文件。您的文件將在最後自動關閉。
  2. 該文件被用作迭代器,因爲在這種情況下沒有理由將整個文件讀入內存中
  3. 對於每行使用回車,不需要執行int(line.strip())int先剝去空白,然後int('12\r\n')正常工作
  4. pList+(int(line.strip()))可能是TypeError,因爲您無法將int連接到列表。改爲使用pList.append(int(line))