2017-05-03 7 views
0

私はファイル名が.javaで、ファイル名に "Application"が含まれているファイルを開きます。ファイルは正しくアクセスされていますが、ファイルを読み込もうとすると空の文字列が表示されます。ファイルを開く空を返します

ファイルが空ではなく、application_class.tell()は0を返します(これは、ファイルの先頭にポインタの読み込みがあることを意味します)。私のファイルを読むために何ができますか?

import os 

files = [f for f in os.listdir('.') if os.path.isfile(f)] 
filename_application = "" 

for file in files: 
    if "Application" in file: 
     filename_application = file 
application_class = open(filename_application, "r") 
#application_class.seek(0) 
print(application_class.tell()) 
print(application_class.read()) 

あなたはファイルの最後にseek()に必要

$ ls 
CustomersApplication.java main.py 
+0

を私は、これはの「メイン」で、あなたはディレクトリ – TLOwater

+0

ごとに1つのだけのアプリケーションファイルを期待していない限り、私は唯一のアプリケーションファイルを期待して、あなたのインデントを変更する必要があると思います私のJavaプロジェクト。 –

+0

テストから、Javaファイルを読み取るだけのようには見えない – TLOwater

答えて

0

application_class.tellは、()正しい現在位置を返します。 read()とtell()の順序を変更してみてください。

また、プログラムを簡素化することができます。

import os 

files = [f for f in os.listdir('.') if os.path.isfile(f) and "Application" in f] 

filename_application = files[0] if files else "" 
if not filename_application: 
    print "Application not found!" 
else: 
    application_class = open(filename_application, "r") 
    print(application_class.read()) 
    print(application_class.tell()) 
+0

私のapplication_class.read()は空の文字列を返します:-( –

+0

'open'また、filename_applicationを印刷して、これが後のファイルであることを確認してください。 – Ashalynd

1

サイズtell()に私のディレクトリ:

application_class.seek(0, os.SEEK_END) 
print(application_class.tell()) 
関連する問題