Hua Cha
Hua Cha

Reputation: 117

How do I check for a file and then move to hdfs?

I'm getting a file at a location "/tmp". Another process will place files to the temp location. I don't know the name of the file. I need to write a script to check if a file exists. If it does, then move it to an HDFS location, otherwise exit.

I guess question 1 is: Are there wild characters in Python? If so, I can just do

os.path.isfile("/tmp/mike/*")

But I realized * doesn't work.

Or if I have keywords. How do I search by keywords?

Thanks.

Upvotes: 0

Views: 73

Answers (1)

Harsha
Harsha

Reputation: 363

There might be an easier way.

If you want to search by keyword in the file name.

tRoot = "__path to temp dir"
keyword = "You keyword here"
for root, dirs, files in os.walk(tRoot):
    for file in files:
        if keyword in file:
            fullPath = str(os.path.join(root, file))
            #file is the filename

If you want to search by extension.

tRoot = "__path to temp dir"
extension = "You extension here"
for root, dirs, files in os.walk(tRoot):
    for file in files:
        if file.endswith(extension):
            fullPath = str(os.path.join(root, file))
            #file is the filename

Upvotes: 1

Related Questions