Python: Check folder directory given by user input

MetalMuzu

I'm creating some python code, where its initial stage lets the user input the directory of a source folder, destination folder and a text file. These directories should be checked right after the user input. Here is an example how it should look like:

Enter directory path of a source folder:
>>>C:\bla\source_folder
Thanks, this folder exists.

Enter directory path of a destination folder:
>>>C:\dest_folder            

(let's say this folder does not exist)

This folder does not exist! Please enter correct path.
>>>C:\bla\dest_folder

Enter directory path of a text file:
>>>C:\bla\bla.txt
Thanks, this file exists.

My code is incomplete, and probably wrong, because I really don't know how to do it.

def source_folder()
    source_folder = raw_input("Enter path of source folder: ")
    try:
        if open(source_folder)
            print("Folder path is correct!")
    except:
        #if there are any errors, print 'fail' for these errors
        print(source_folder, 'This folder has not been found')

def dest_folder()
    dest_folder = raw_input("Enter path of destination folder: ")

def input_file()
    input_file = raw_input("Enter path of your text file: ")
MetalMuzu

Together with @massiou's advice I completed this script as follows:

import os

found = False
source_folder=None
dest_folder=None
text_file=None


while not found:
    source_folder = str(raw_input("Enter full path of source folder: "))
    print source_folder
    if not os.path.isdir(source_folder):
        print(source_folder, 'This folder has not been found. Enter correct path. ')
    else:
        print("Folder path is correct!")
        found = True

found = False
while not found:
    dest_folder = raw_input("Enter full path of destination folder: ")
    if not os.path.isdir(dest_folder):
        print(dest_folder, 'This folder has not been found. Enter correct path. ')
    else:
        print("Folder path is correct!")
        found = True

found = False
while not found:
    text_file = raw_input("Enter full path your text file folder: ")
    if not os.path.isfile(text_file):
        print(text_file, 'This file has not been found. Enter correct path. ')
    else:
        print("File path is correct!")
        found = True

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to check size of a directory given by user as an argument?

From Dev

Check if a given directory contains any directory in python

From Dev

In Ruby 1.9.3, check if the user input is a directory

From Dev

Python: Check if any file exists in a given directory

From Dev

How to check user input (Python)

From Dev

How to check user input (Python)

From Dev

Restrict user to given directory

From Dev

Python 3: Specific user input check

From Dev

Check if input is a file or folder

From Dev

How to check if folder exist inside a directory if not create it using python

From Dev

Check if the current folder is the root directory

From Dev

Check if user input is in array

From Dev

Check if the user input fits

From Dev

Python folder names in the directory

From Dev

Creating folder with user input : Android

From Dev

User input - folder and subfolder creation

From Dev

Check if file or folder by given path exists

From Dev

Check if a given path is a special folder path?

From Dev

python subprocess: check to see if the executed script is asking for user input

From Dev

Python how can you check for consecutive letters in a user input?

From Dev

How can I check if a given directory is accessible?

From Dev

Check if a file exists in a given directory, NOT subdirectories

From Dev

How to check if a given path is a file or directory?

From Dev

Check if a directory contains a file with a given extension

From Dev

Check existence of files with a given extension in a directory

From Dev

Check if mp3 file is in given directory

From Dev

User Input file directory with spaces?

From Dev

How do I access a file in a sub-directory on user input without having to state the directory in Python 2.7.11?

From Dev

Check if the User is valid user on Domain - Active Directory

Related Related

  1. 1

    How to check size of a directory given by user as an argument?

  2. 2

    Check if a given directory contains any directory in python

  3. 3

    In Ruby 1.9.3, check if the user input is a directory

  4. 4

    Python: Check if any file exists in a given directory

  5. 5

    How to check user input (Python)

  6. 6

    How to check user input (Python)

  7. 7

    Restrict user to given directory

  8. 8

    Python 3: Specific user input check

  9. 9

    Check if input is a file or folder

  10. 10

    How to check if folder exist inside a directory if not create it using python

  11. 11

    Check if the current folder is the root directory

  12. 12

    Check if user input is in array

  13. 13

    Check if the user input fits

  14. 14

    Python folder names in the directory

  15. 15

    Creating folder with user input : Android

  16. 16

    User input - folder and subfolder creation

  17. 17

    Check if file or folder by given path exists

  18. 18

    Check if a given path is a special folder path?

  19. 19

    python subprocess: check to see if the executed script is asking for user input

  20. 20

    Python how can you check for consecutive letters in a user input?

  21. 21

    How can I check if a given directory is accessible?

  22. 22

    Check if a file exists in a given directory, NOT subdirectories

  23. 23

    How to check if a given path is a file or directory?

  24. 24

    Check if a directory contains a file with a given extension

  25. 25

    Check existence of files with a given extension in a directory

  26. 26

    Check if mp3 file is in given directory

  27. 27

    User Input file directory with spaces?

  28. 28

    How do I access a file in a sub-directory on user input without having to state the directory in Python 2.7.11?

  29. 29

    Check if the User is valid user on Domain - Active Directory

HotTag

Archive