Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
258 views
in Technique[技术] by (71.8m points)

Strange issue on retrieving a proportion of files from a directory: No such file or directory error in python

I know this may be a repetitive question but it has a special point which i can't find out.
I'm trying to retrieve files from a directory in my python application. The function below retrieves a proportion of files based on testOrTrain variable. Here is my code:

def get_files_in_dir(directory,trainOrTest):
    file_names_list = []
    for file_name in glob.glob(directory):
        file_names_list += [file_name]

   #1 for train_data and 0 for test_data
    if trainOrTest == 1:
      proportion = len(os.listdir(directory))*65/100
      return file_names_list[:int(proportion)]
    elif trainOrTest == 0:
      proportion = len(os.listdir(directory))*35/100
      return file_names_list[int(proportion):]
    # return file_names_list

And here is the function in which i use it:

def training_data():
   for i in range(1,2):
     p = 'C'+'%.2d' % i
     file_path = os.path.join('ohsumed-all',str(p),'*')
     for file in get_files_in_dir(file_path,1):
        file_data = get_text_file_data(file)
        print(file_data)

This code gives me the error "No such file or directory". The strange thing about this code is that when i delete the if parts and just simply return files_names_list(the part of code in the comment), it works fine.
Does somebody know what is happening here?
By the way, i'm working on wsl(ubuntu)

question from:https://stackoverflow.com/questions/65872368/strange-issue-on-retrieving-a-proportion-of-files-from-a-directory-no-such-file

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...