Checking for file existence.
See original GitHub issueAs we use s3 for storage of generated assets, and only generate them again if not existing yet, we need to implement a check for existence.
How about a from smart_open import exists
?
It would be great if this library could also allow easy checking for existence, tailored for it’s multiple backends.
from smart_open import exists # like os.exists
if not exists("s3://my_key:my_secret@my_server:my_port@my_bucket/some_file.mp4"):
data = long_running_procedure("some_file.mp4") # generate assets
from smart_open import open
with open("s3://my_key:my_secret@my_server:my_port@my_bucket/some_file.mp4", "wb") as f:
f.write(data)
# end with
print("Generated from scratch.")
else:
print("Already uploaded.")
# end if
Issue Analytics
- State:
- Created 4 years ago
- Comments:12
Top Results From Across the Web
How to Check If a File Exists in Python
1) Using os.path.exists() function to check if a file exists ... To check if a file exists, you pass the file path to...
Read more >How do I check whether a file exists without exceptions?
Use os.path.isfile to check only files and Use os.path.exists to check both files and directories. Learn more from here: shortbuzz.in/blog/shortbuzz.in/…
Read more >Python Check if File Exists: How to Check If a Directory Exists?
Python exists() method is used to check whether specific file or directory exists or not. It is also used to check if a...
Read more >Python - Check if a file or directory exists - GeeksforGeeks
Using os.path.exists() to check if file exists · Python3 · Using os.path.isfile() Method to check if file exists · Python3 · Using os.path.isdir() ......
Read more >Python Check If File Exists [3 Ways] - PYnative
Use the os.path.isfile('file_path') function to check whether a file exists. Pass the file name or file path to this function as an argument....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
You don’t need to download it. Smart_open streams its data. If you open a stream, but don’t actually read from it, nothing gets downloaded.
Try:
The ValueError gets raised here in case you want to do more precise checking.
Using
boto3
, which also is internally used bysmart_open
, we used the listing withPrefix=file_path
to check for existence, as it doesn’t download the file, and we have thes3:ListBucket
permission set anyway.Source: Stackoverflow, also includes other solutions with probably don’t require
s3:ListBucket
.