Support Questions
Find answers, ask questions, and share your expertise
Announcements
Alert: Welcome to the Unified Cloudera Community. Former HCC members be sure to read and learn how to activate your account here.

File Overwrite not working with HDFS NFS gateway through python.

File Overwrite not working with HDFS NFS gateway through python.

Contributor

I am trying to create and overwrite a file in file system. The below code working with local filesystem and NFS as well.

But not working with HDFS NFS gateway.

open_file = open("/hdfs_nfs/hdfs_Data/sampledata/testWrite.txt", 'wb+')
open_file.write("This is just a sample data")
open_file.close()

The file "testWrite.txt" is getting created on HDFS mount point when i run above code for first time. But it is not working when I run the code from 2nd time onwards, with or with out changing the content.

I am getting below error.

IOError: [Errno 22] invalid mode ('wb+') or filename: '/hdfs_nfs/hdfs_Data/sampledata/testWrite.txt'
1 REPLY 1
Highlighted

Re: File Overwrite not working with HDFS NFS gateway through python.

Expert Contributor

@Paramesh malla

According to the official documentation "file append is supported but random write is not supported". However, you very much can remove the file if it already exists and create a new one:

import os

path = '/hdfs_nfs/hdfs_Data/sampledata/testWrite.txt'
try:
    os.remove(path)
except OSError:
    pass

with open(path, 'wb+') as open_file:
    open_file.write("This is just a sample data")
Don't have an account?
Coming from Hortonworks? Activate your account here