1

I'm writing a program that selects a random folder in a file and opens it. However, it works most of the time, but when it does not select a file, it crashes with the following error (here's the snippet of the code)...

File "C:\Users\rbloc\OneDrive\Documents\Thesis\Python Scripts\FileRO.py", line 51, in fileopener mystat = os.stat(randomFile)

builtins.FileNotFoundError: [WinError 3] The system cannot find the path specified: ''

Here is the code:

def fileopener(logFile, StartTime, CloseTime):

#for loop through specified directory and copy append names of files into list
for root, dirs, files in os.walk(r"Y:\Documents\Data", topdown=True):
    #time.sleep(randint(5,15))
    for file in files:
        
        file_list.append(os.path.join(root, file))

        #select random file from the file list generated
        randomFile = file_list[np.random.choice(len(file_list))]

I know the error is occurring on the last line, but I can't seem to fix it. Does anyone have a solution?

EDIT WITH NEW CODE:

So I removed the random function that caused the issue and just have the program open files using the for-loop and works great. So I've narrowed it down to the random function, as suspected, that is causing the program the crash when it does not select a file....

def fileopener(logFile, StartTime, CloseTime):

#for loop through specified directory and copy append names of files into list
for root, dirs, files in os.walk(r"Y:\Documents\Data", topdown=True):
    #time.sleep(randint(5,15))
    
    for file in files:
    
        randomFile = os.path.join(root, file)

        #print time stamp for operation that opened and open random file/folder selected
        print("Time OPENED ", timeopen(), " file is: ", randomFile)
Ray
  • 33
  • 4
  • You can try to print a path before opening it. So you can at least check if the file is really present or not. – Michael Butscher Nov 18 '20 at 17:54
  • @MichaelButscher so the thing is, there are files in the path. I just need it to select a random file from the directory. But what it does it chooses not to select a file sometimes. It should always select a file at random since the directory has files in it. – Ray Nov 18 '20 at 17:56
  • you can move **random.choice** after the loop to expect your **file_list** to contain all files and validate if **len(filelist)> 0** because if it equals to 0 **random choice** generates an error. – Erick Cruz Nov 18 '20 at 18:52
  • The argument to `numpy.random.choice` should be the array you want to pick an item from, not a number. Voting to close as typo. (You hardly need `numpy` to generate a single random integer anyway.) – tripleee Nov 18 '20 at 18:52
  • @tripleee can you recommend an alternative to selecting a random file function? – Ray Nov 18 '20 at 18:58
  • https://stackoverflow.com/questions/64040496/how-to-choose-a-random-element-from-a-list-and-then-find-its-index-in-the-list – tripleee Nov 18 '20 at 19:12

1 Answers1

1

This code works for me

import os
import numpy as np

def fileopener(logFile, StartTime, CloseTime):
    file_list = []
    #for loop through specified directory and copy append names of files into list
    for root, dirs, files in os.walk("Y:\Documents\Data", topdown=True):
        #time.sleep(randint(5,15))
        for file in files:
            file_list.append(os.path.join(root, file))

    #select random file from the file list generated
    randomFile = file_list[np.random.choice(len(file_list))]
    print(randomFile)
Tom
  • 918
  • 6
  • 19