Click here to Skip to main content
15,911,531 members

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'