Support Questions

Find answers, ask questions, and share your expertise

File Overwrite not working with HDFS NFS gateway through python.


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")

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'

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'
except OSError:

with open(path, 'wb+') as open_file:
    open_file.write("This is just a sample data")