15,911,531 members
Sign in
Sign in
Email
Password
Forgot your password?
Sign in with
home
articles
Browse Topics
>
Latest Articles
Top Articles
Posting/Update Guidelines
Article Help Forum
Submit an article or tip
Import GitHub Project
Import your Blog
quick answers
Q&A
Ask a Question
View Unanswered Questions
View All Questions
View C# questions
View C++ questions
View Javascript questions
View Visual Basic questions
View Python questions
discussions
forums
CodeProject.AI Server
All Message Boards...
Application Lifecycle
>
Running a Business
Sales / Marketing
Collaboration / Beta Testing
Work Issues
Design and Architecture
Artificial Intelligence
ASP.NET
JavaScript
Internet of Things
C / C++ / MFC
>
ATL / WTL / STL
Managed C++/CLI
C#
Free Tools
Objective-C and Swift
Database
Hardware & Devices
>
System Admin
Hosting and Servers
Java
Linux Programming
Python
.NET (Core and Framework)
Android
iOS
Mobile
WPF
Visual Basic
Web Development
Site Bugs / Suggestions
Spam and Abuse Watch
features
features
Competitions
News
The Insider Newsletter
The Daily Build Newsletter
Newsletter archive
Surveys
CodeProject Stuff
community
lounge
Who's Who
Most Valuable Professionals
The Lounge
The CodeProject Blog
Where I Am: Member Photos
The Insider News
The Weird & The Wonderful
help
?
What is 'CodeProject'?
General FAQ
Ask a Question
Bugs and Suggestions
Article Help Forum
About Us
Search within:
Articles
Quick Answers
Messages
Comments by Hanginium2412 (Top 5 by date)
Hanginium2412
28-Mar-24 3:13am
View
Thank you for being so helpful, Andre Oosthuizen. I refactored my code as you suggested, but I got the following error:
file_contents = file_tar_bz2.extractfile(file_info).read()
AttributeError: 'NoneType' object has no attribute 'read'.
After some reading, I understood that this issue occurs if the individual file in the tar archive is not a regular file or link, therefore it's considered None. I wrote some if statements to allow objects such as a regular file, directory, and symbolic or hard link to be considered, read, and considered others as None and skip them. However, I keep getting the same error above. Could you please help solve this issue and see where I am wrong?
# Create a new object with the contents of the compressed Netbox media backup
with tarfile.open("/var/backup/netbox_backups/netbox_media_2024-03-24.tar.bz2", "r:bz2") as file_tar_bz2:
# Go over each file in the tar archive...
for file_info in file_tar_bz2:
if file_info.isreg():
# Read the contents...
logger.info(f"Is regular file: {file_info.name}")
file_contents = file_tar_bz2.extractfile(file_info).read()
elif file_info.isdir():
# Read the contents...
logger.info(f"Is directory: {file_info.name}")
file_contents = file_tar_bz2.extractfile(file_info).read()
elif file_info.issym():
# Read the contents...
logger.info(f"Is symbolic link: {file_info.name}")
file_contents = file_tar_bz2.extractfile(file_info).read()
elif file_info.islnk():
# Read the contents...
logger.info(f"Is hard link: {file_info.name}")
file_contents = file_tar_bz2.extractfile(file_info).read()
else:
logger.info(f"Is something else: {tarinfo.name}. Skip it")
continue
# Create a file-like object from the contents...
file_like_object = io.BytesIO(file_contents)
# Upload the returned contents to Swift...
swift_conn.put_object(
container,
file_info.name,
# Use the name of the file selected in the archive as your object name...
contents=file_like_object,
content_type='application/x-tar' # Set the appropriate content type...
)
Below is the error I got after running the script
File "/opt/scripts/netbox_backups_transfer.py", line 69, in <module>
file_contents = file_tar_bz2.extractfile(file_info).read()
AttributeError: 'NoneType' object has no attribute 'read'
Hanginium2412
28-Mar-24 3:12am
View
Good morning everyone. I hope you all are doing okay. Can somebody kindly help me with this issue? It's been more than 3 days, and I haven't received any feedback yet and I've been battling it for quite some time now.
I refactored my code as you suggested, but I got the following error:
file_contents = file_tar_bz2.extractfile(file_info).read()
AttributeError: 'NoneType' object has no attribute 'read'.
After some reading, I understood that this issue occurs if the individual file in the tar archive is not a regular file or link, therefore it's considered None. I wrote some if statements to allow objects such as a regular file, directory, and symbolic or hard link to be considered, read, and considered others as None and skip them. However, I keep getting the same error above. Could you please help solve this issue and see where I am wrong?
# Create a new object with the contents of the compressed Netbox media backup
with tarfile.open("/var/backup/netbox_backups/netbox_media_2024-03-24.tar.bz2", "r:bz2") as file_tar_bz2:
# Go over each file in the tar archive...
for file_info in file_tar_bz2:
if file_info.isreg():
# Read the contents...
logger.info(f"Is regular file: {file_info.name}")
file_contents = file_tar_bz2.extractfile(file_info).read()
elif file_info.isdir():
# Read the contents...
logger.info(f"Is directory: {file_info.name}")
file_contents = file_tar_bz2.extractfile(file_info).read()
elif file_info.issym():
# Read the contents...
logger.info(f"Is symbolic link: {file_info.name}")
file_contents = file_tar_bz2.extractfile(file_info).read()
elif file_info.islnk():
# Read the contents...
logger.info(f"Is hard link: {file_info.name}")
file_contents = file_tar_bz2.extractfile(file_info).read()
else:
logger.info(f"Is something else: {tarinfo.name}. Skip it")
continue
# Create a file-like object from the contents...
file_like_object = io.BytesIO(file_contents)
# Upload the returned contents to Swift...
swift_conn.put_object(
container,
file_info.name,
# Use the name of the file selected in the archive as your object name...
contents=file_like_object,
content_type='application/x-tar' # Set the appropriate content type...
)
Below is the error I got after running the script
File "/opt/scripts/netbox_backups_transfer.py", line 69, in <module>
file_contents = file_tar_bz2.extractfile(file_info).read()
AttributeError: 'NoneType' object has no attribute 'read'
Hanginium2412
25-Mar-24 2:40am
View
Deleted
Thank you for being so helpful, Andre Oosthuizen. I refactored my code as you suggested, but I got the following error:
file_contents = file_tar_bz2.extractfile(file_info).read()
AttributeError: 'NoneType' object has no attribute 'read'.
After some reading, I understood that this issue occurs if the individual file in the tar archive is not a regular file or link, therefore it's considered None. I wrote some if statements to allow objects such as a regular file, directory, and symbolic or hard link to be considered, read, and considered others as None and skip them. However, I keep getting the same error above. Could you please help solve this issue and see where I am wrong?
# Create a new object with the contents of the compressed Netbox media backup
with tarfile.open("/var/backup/netbox_backups/netbox_media_2024-03-24.tar.bz2", "r:bz2") as file_tar_bz2:
# Go over each file in the tar archive...
for file_info in file_tar_bz2:
if file_info.isreg():
# Read the contents...
logger.info(f"Is regular file: {file_info.name}")
file_contents = file_tar_bz2.extractfile(file_info).read()
elif file_info.isdir():
# Read the contents...
logger.info(f"Is directory: {file_info.name}")
file_contents = file_tar_bz2.extractfile(file_info).read()
elif file_info.issym():
# Read the contents...
logger.info(f"Is symbolic link: {file_info.name}")
file_contents = file_tar_bz2.extractfile(file_info).read()
elif file_info.islnk():
# Read the contents...
logger.info(f"Is hard link: {file_info.name}")
file_contents = file_tar_bz2.extractfile(file_info).read()
else:
logger.info(f"Is something else: {tarinfo.name}. Skip it")
continue
# Create a file-like object from the contents...
file_like_object = io.BytesIO(file_contents)
# Upload the returned contents to Swift...
swift_conn.put_object(
container,
file_info.name,
# Use the name of the file selected in the archive as your object name...
contents=file_like_object,
content_type='application/x-tar' # Set the appropriate content type...
)
Below is the error I got after running the script
File "/opt/scripts/netbox_backups_transfer.py", line 69, in <module>
file_contents = file_tar_bz2.extractfile(file_info).read()
AttributeError: 'NoneType' object has no attribute 'read'
Hanginium2412
23-Mar-24 13:01pm
View
Good day Andre. Thank you for your response. I will gladly appreciate your help whenever you have time this weekend as this issue has been stressing me out for days and I would want it to be resolved asap.
Hanginium2412
22-Mar-24 8:15am
View
Thank you for your help Andre Oosthuizen and Peter O'Hanlon. I refactored my code as you suggested, but I got the error regarding the 'TarFile' object having no attribute 'read' after running my script.
# Create a new object with the contents of the compressed Netbox media backup
with tarfile.open("/var/backup/netbox_backups/netbox_media_2024-03-16.tar.bz2", "r:bz2") as file_tar_bz2:
# Read the contents of the compressed Netbox media backup file
file_contents = file_tar_bz2.read()
# Create a file-like object from the contents of the compressed Netbox media backup file
my_file_like_object = io.BytesIO(file_contents)
# Upload the returned contents to the OpenStack Object Storage container
swift_conn.put_object(
container,
'object_netbox_media_2024-03-16.tar.bz2',
contents=file_tar_bz2,
content_type='application/x-tar'
)
Below is the error I got after running the script. My apologies for these questions, I'm still a beginner in Python.
Python
Traceback (most recent call last):
File "/opt/scripts/netbox_backups_transfer.py", line 57, in <module>
file_contents = file_tar_bz2.read()
AttributeError: 'TarFile' object has no attribute 'read'