Acess local_directory

Acess local_directory

Acess local_directory

Hi coders,

This is my code to view the local directory, files your local system.

To begin with explaination:

In this code first we need to import some module are OS,RE(regular expersion),SYS(system).

1.The OS module in Python provides functions for interacting with the operating system

2.The sys module in Python provides various functions and variables that can be used to manipulate different parts of the Python runtime environment

3.The RE is used to filter or search a particular word

Lets start,input function used to get a input from user and store in enter_file_location(variable).Os.listdir function interact with operating system and display folder,files based on user_input(enter_file_location).Suppose user enter wrong location the program just exit. os.path.splitext function used to split a txt name and txt extension.Dict is used to collect a extension you can add any extension you want,in this code,I used some logic to display a output message you can understand by seeing it.

Note:If there any bug please contact me




import  os,re,sys

# need to get a file_location as an input

enter_file_location=input("enter the file_location(ex:C:\):")

cc = enter_file_location

print(enter_file_location)

# display  a files in the dict it based on a user input

try:

 c = os.listdir(cc)

 print(c)

except:

    print("please enter crt location")

    sys.exit()

# collect the filename form the user

enter_file = input("enter the file name:")

# set a function

def fun(cc,enter_file): # passing arguments in function

   try:

     file = open(cc+ '/'+ enter_file,"r") #reading the inputs file

   except FileNotFoundError as e:# file is not found in the folder

     print(e)

     sys.exit()



   count = 0

   ext=os.path.splitext(enter_file) # for separating the extension

   print("ext[0]:",ext[0],"ext[1]:" ,ext[1])

   for i in enter_file: # avoid some file which contain two dot ex:(.py.txt)just to
                        #eliminate based on two extensions

        if   i ==".":

            count+=1

   if re.search(".tar",ext[0]) and ext[1]==".gz": # we need to give a extension(.txt ,.xml) 
                                                  #suppose you need to give specific two extensions

        count = 1

   if count == 1:

          flag = False

          dict=[".xml",".py",".json",".txt",".gz"] # add a extension

          for k in dict:

             if re.search(k,ext[1]): #using regular expression

                print("\n its works")

                flag = True

          if flag == False:

                print("\n its wrong")

   elif count > 1:

            print("you out")

fun(cc,enter_file)

0 Response to "Acess local_directory"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel