0

This is the beginning of my code that takes a dataset and plots it using matplotlib. However, I want to create a while loop that prompts the user to provide the correct path or directory to the file (e.g. /Users/Hello/Desktop/file.txt).

While the user does not inputs a correct path, the loop should keep prompting for a directory.

If there is indeed a file in that path, it should store this input in the variable path, which is later used to display the data.

If there is no file, it should ask it again.

It seems that my while loop is stuck at the first print statement..

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import itertools
import os


# This is the correct directory: os.path.isfile('/Users/Hello/Desktop/file.txt')

""" 
Prompt the user for the right directory to the file.
If the file is not there, ask again.
If the path is correct, store it in the variable Path.
"""

correct = os.path.isfile(directory)

directory = input("Please provide the correct directory to the dataset  ")

    while os.path.isfile == False:

        if os.path.isfile(Directory) != True:
            directory = ath
            print('Thank you')
        elif os.path.isfile(Directory) == True:
            print("This directory does not exist, please try again.")


#this variable inserts the users input and displays the data in the file

dframe = pd.read_table(path, delim_whitespace=True, names=('X','Y'),
                   dtype={'X': np.float64, 'Y':np.float64})

dframe.head(10) # show the first 10 rows of the data
Steve
  • 303
  • 1
  • 10
  • your loop condition does not make sense. `os.path.isfile` is a function, expecting a path to check. also you would need to call the `directory = input...` inside the while-loop again to actually retrieve another input. – Dave J Apr 30 '18 at 20:35
  • Why don't you use a filedialog so the user inputs the file? – diegoiva Apr 30 '18 at 20:35
  • In the while loop you need to hand over the directory `os.path.isfile(directory)` in the if and elif statement use use lowercase 'd' to match with your variable – MBT Apr 30 '18 at 20:36
  • If this is for desktop use, the python standard library contains `tkinter.filedialog`, which provides a cross-platform gui dialogue window that you could use. https://stackoverflow.com/questions/9319317/quick-and-easy-file-dialog-in-python – Håken Lid Apr 30 '18 at 20:38
  • 1
    Possible duplicate of [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – SiHa Apr 30 '18 at 20:42
  • @HåkenLid I tried implementing this, and while it works, it gives me the spinning wheel every time, so I have to quite the program every time. – Steve May 01 '18 at 07:32

2 Answers2

1

Write a function. Your code has multiple problems, but I guess you want something like this.

def prompt_for_filepath():
    """ 
    Prompt the user for the right path to the file.
    If the file is not there, ask again.
    If the path is correct, return it.
    """
    while True:        
        path = input("Please provide the correct path to the dataset")    
        if os.path.isfile(path):
            return path
        print("This path does not exist, please try again.")
Håken Lid
  • 18,252
  • 8
  • 40
  • 58
0

The code that sample that you posted is not syntactically correct.

  • The while block should not be indented compared to the preceding line.
  • directory is used before it is defined

To answer your question directly, after print('Thank you'), you need to have a break statement to break out of the while loop.

Your while loop invariant is also incorrect-- you're checking if os.path.isfile is False. os.path.isfile is a function and functions are truthy. You effectively wrote while True:-- this isn't incorrect, but it's not doing what you probably think it's doing.

Ben Yarmis
  • 111
  • 7