I'm writing a program that scans for specific file types and reads them for certain 'commands'. I have the code filenameglob = (glob.glob("*.doremi"))
but upon returning the variable with print
it comes out as ['test.doremi']
instead of the optimal test.doremi
. The problem with this is I can not use it to read the file. How would I go about using glob to search for the file, output the name and then read the file with that name?
glob.glob
returns a list of items matching the search criteria passed at runtime- or an empty list if no matches are found. This is why you are getting ['test.doremi']
instead of test.doremi
.
The filenameglob
variable in your example is a list containing 1 element - the string test.doremi
- which can be accessed using it's index within the list ie filenameglob[0]
is test.doremi
.
The below code provides an appropriate answer/solution to your original question assuming that there will be 0 or 1 .doremi
files in the target directory.
import glob
filenameglob = glob.glob("*.doremi")
if filenameglob: # If glob.glob returned list of matches
filename = filenameglob[0] # Take first item from filename glob
print(filename)
with open(filename, "r") as f: # Open file in read mode
contents = f.read() # Read contents from file to variable
# Do something with contents read from .doremi file
else: # Else if glob.glob returned empty list
print("No .doremi file found!")
# Define behaviour in case of no .doremi file found
.doremi
files from directoryThe above code assumes 0 or 1 files with the .doremi
extension within the execution directory as per your original question.
As described above, glob.glob
returns a list of strings representing matches based on the arguments passed to the function at runtime - in this case '*.doremi'
.
Therefore it is probably worth considering how you will handle multiple .doremi
files should they be found within the execution directory by glob.glob
.
One solution may be to write a function that defines how you will handle contents read from each .doremi
file and call this within a loop for each item returned by glob.glob
. See below for an example of how you might do this.
import glob
def process_contents(contents):
# Process file contents here
filenameglob = glob.glob("*.txt")
if filenameglob: # If glob.glob returned list of matches
for filename in filenameglob: # For each file in filenameglob list
print(filename)
with open(filename, "r") as f: # Open current file in read mode
contents = f.read() # Read contents from current file to variable
process_contents(contents) # Send contents from file to function for processing
else: # Else if glob.glob returned empty list
print("No .doremi files found!")
# Define behaviour in case of no .doremi files found
The above code will work for any number (0...n) of files returned by glob.glob
, as such I would recommend taking this approach as to better mitigate the risk of your code processing the "wrong" .doremi
file - as may happen in the Basic Answer as it will only process the first in the list, even if multiple are present.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.