I would like to check if a file is present in a particular folder and print the output. I have the following files in the path: ./programs/data/my_files:
data.txt
an_123.txt
info.log
an_234.txt
filename.txt
main.py
an_55.txt
I would like to check if the files data.txt, filename.txt, and an_55.txtis present in the path ./programs/data/my_files. The output should be the following:
Success: data.txt exists
What I tried so far?
pth = str("./programs/data/my_files/")
filename = ['data.txt', 'filename.txt', 'an_55.txt']
for i in filename:
if glob.glob(os.path.join(pth)):
print('Success: ', "NaN_"+i+".xml exists")
else:
print('Failure: ', "NaN_"+i+".xml does not exists")
This prints success only for the last item in the list, (i,e, an_55.txt), others are failure. How do I correct this?
Try this
import os
files = os.listdir("your/path/to/files")
for file in files:
if file.startswith("data") # you can also check if the item is a file here os.isfile()
print("success")
You can also use
os.path.exists("path to a file")
This might be helpful
from pathlib import Path
my_file = Path("/path/to/file") #add your path lists
if my_file.is_file():
print("present")
else:
print("not present")
Related
I have a path which have many directories. I need to go through each directory and get a specific file "file.log.gz" from it and read the file and do some process.
This is my current attempt:
import os
import sys
import gzip
infile = sys.argv[1]
directory = ("%s/NEW_FOLDER" % infile)
for root, dirs, files in os.walk(directory):
for file in files:
if "file.log.gz" in file:
with gzip.open(os.path.join(root, file)) as fin:
new = False
for line in fin:
if "CODE" in line.decode('utf-8'):
print("string is present")
found = True
exit()
else:
print("string is not present")
what i need is to go through each directories inside NEW_FOLDER and get file.log.gz. and do the following process for file.log.gz in each directory.
with the current code i get file.log.gz inside each directory but i'm not able to do rest of the process that is opening file.log.gz in each directory and do the rest process.
Expected Output:
/NEW_FOLDER/dir1/file.log.gz
string is present
/NEW_FOLDER/dir2/file.log.gz
string is present
/NEW_FOLDER/dir3/file.log.gz
string is not present
Because you are using os.walk(). You need to merge the root directory with the filename. You will notice it if you print (file) and see what the values you are getting.
Try print this out. You suppose to pass the entire directory to open and not just the file name.
for file in files:
print(os.path.join(root, file))
Consider i have 5 files in 5 different location.
Example = fileA in XYZ location
fileB in ZXC location
fileC in XBN location so on
I want to check if these files are actually saved in that location if they are not re run the code above that saves the file.
Ex:
if:
fileA, fileB so on are present in their particular location the proceed with code further
else:
re run the file saving code above
How do i do this in python i am not able to figure out.
You can store all your files with their locations in a list and then iterate all locations for existence then you can decide further what to do.
A Python example:
from os.path import exists
# all files to check in different locations
locations = [
'/some/location/xyz/fileA',
'/other/location/fileB',
'/yet/another/location/fileC',
]
# iterate to check each file existance
status = [exists(location) for location in locations]
# check the status of all files
# if any of the files doesn't exist, else will be called
if(all(status)):
print('All files are present.')
else:
print('Any or all files do not exist.')
I'm not a python dev, and I just wanted try to contribute to the community.
The first answer is way better than mine, but I'd like to share my solution for that question.
You could use sys to pass the files' names, inside a try block to handle when the files are not found.
If you run the script from a location while the files are at another location, it would be needed to provide their path.
check.py ../test1.txt ../test2.txt ../test3.txt
#!/usr/bin/python3
import os.path
import sys
try:
fpath = sys.argv[1]
fpath = sys.argv[2]
fpath = sys.argv[3]
if (os.path.isfile(fpath)) != "":
print("Exists on system")
else:
pass
except IndexError:
for i in range(3):
file1 = "test1.txt"
file2 = "test2.txt"
file3 = "test3.txt"
sys.stdout = open(file1, "w")
print("Saving content to files")
sys.stdout = open(file2, "w")
print("Saving content to files")
sys.stdout = open(file3, "w")
print("Saving content to files")
The exception part would then "save" the files, by creating new ones, writing whatever content you desire.
I don't think that the following code is efficient enough to search for a filename in the current directory. The filename will be stored as a string, so will python be able to search a 'filename' string from the directories that are non-string?
filename = input("What would you like to name the File? ")
import os
if f"{filename}.txt" in os.getcwd():
print(True)
os.getcwd() returns the name of the directory itself, not a list of files (see https://www.tutorialspoint.com/python/os_getcwd.htm)
You want to try something like:
import os
for file in os.listdir("/mydir"):
if file == f"{filename}.txt:
print("File Found")
Also, you need to try-except statement for this code snippet. Otherwise, at the wrong file, the app will be crashed.
import os
# Getting current folder path
cmd = os.getcwd()
# getting file name from user
file_name = input('Enter file name to be search : ')
#Looping through all folder and files in the current directory
for file in os.listdir(cmd):
# compare the file name user entered with file names from current
# directory file name
if file == f"{file_name}.txt":
print("File Found")
I'm trying to loop through some files in a directory. If the filename has two specific strings together, then I'm supposed to open and read those files for information. However, if none of the files have those two strings, I want to print an error message only once.
for filename in os.listdir(directory):
if filename.find("<string1>") != -1 and filename.find("<string2>") != -1:
#open file
else:
#print error message
I know doing this will print as many error messages as there are files in the directory (i.e. if there's 15 files with no matches, I'll get 15 error messages). But what I want is to only print an error message once after there aren't any matches in any of the N files in directory. I figured I could do something like this:
for filename in os.listdir(directory):
if filename.find("<string1>") != -1 and filename.find("<string2>") != -1:
#open file
else:
if filename[-1]: #if filename is last in directory
#print error message
But I've discovered this doesn't work. How would I get an error message to print only after the last filename has been read and doesn't match?
A simple solution would be to initialize some boolean flag before your for loop, e.g. found = false
If you find a file, set found = true. Then you can check the value of found after your for loop finishes and print the appropriate message based on its value.
Filter the list of files before the for-loop:
filenames = [fname for fname in os.listdir(directory)
if '<string1>' in fname and '<string2>' in fname]
if filenames:
for filename in filenames:
#open file
else:
#print error message
You can probably also use the glob module to get the filenames:
import glob
filenames = glob.glob(directory + '/*string1*string2*')
Another way is to use a variable to check if all the files have been processed. Checked and found it working in Python 2.7
import os
directory = "E:\\test\\"
files_count = len(os.listdir(directory))
files_processed = 0
for filename in os.listdir(directory):
if 'string1' in filename and 'string2' in filename:
#open file
print ("Opening file")
else:
files_processed = files_processed + 1
if (files_processed >= files_count):
print ("error message")
Not sure if this is extreme. But I'd make it a function and raise IOError.
Plus, i'd always use absolute path. Try the pathlib module too
import os
def get_files(directory):
for filename in os.listdir(directory):
if "string1" in filename and "string2" in filename:
yield filename
raise IOError("No such file")
for file in get_files('.'):
print(file)
# do stuff with file
The purpose of this code is:
Read a csv file which contains a column for a list of file names
here is the csv file:
https://drive.google.com/open?id=0B5bJvxM9TZkhVGI5dkdLVzAyNTA
Then check a specific folder to check if the files exist or not
If its found a file is not in the list delete it
here is the code:
import pandas as pd
import os.path
data = pd.read_csv('data.csv')
names = data['title']
path = "C:\\Users\\Sayed\\Desktop\\Economic Data"
for file in os.listdir(path):
os.path.exists(file)
print(file)
file = os.path.join(path, file)
fileName = os.path.splitext(file)
if fileName not in names:
print('error')
os.remove(file)
I modified the first code, and this is the new code and I got no error but the simply delete all the files in the directory
os.chdir does not return anything, so assigning the result to path means that path has None, which causes the error.
Since you're using pandas, here's a little trick to speed this up using pd.Series.isin.
root = "C:\Users\Sayed\Desktop\Economic Data"
files = os.listdir(root)
for f in data.loc[~data['title'].isin(files), 'title'].tolist():
try:
os.remove(os.path.join(root, f))
except OSError:
pass
Added a try-except check in accordance with EAFP (since I'm not doing an os.path.exists check here). Alternatively, you could add a filter based on existence using pd.Series.apply:
m = ~data['title'].isin(files) & data['title'].apply(os.path.exists)
for f in data.loc[m, 'title'].tolist():
os.remove(os.path.join(root, f))
Your path is the return value of the os.chdir() call. Which is obviously None.
You want to set path to the string representing the path ... leave the chdir out.