Python syntax issue - TypeError: Required argument 'name' (pos 1) not found

Viewed 3103

I am trying to separate some files by their contents and my code looks like this and it keeps showing me the error.

__author__ = 'Sahil Nagpal'

from pyspark import SparkContext,SparkConf
from pyspark.sql import SparkSession


spark = SparkSession.builder.appName("GEOTRELLIS").getOrCreate()
sparkcont = SparkContext.getOrCreate(SparkConf().setAppName("GEOTRELLIS"))
logs = sparkcont.setLogLevel("ERROR")



imageFile = "/mnt/imagefile/IMGFileCopy.txt"
one_meter_File = "/mnt/imagefile/one_meter/one_meter_file.txt"
_13_File = "/mnt/imagefile/13_meter/_13_File.txt"
ned19_File = "/mnt/imagefile/ned19/ned19_File.txt"

#iterate over the file
try:
    with open(file=imageFile,mode="r+") as file, open(file=one_meter_File,mode='w+') as one_meter_File,open(file=_13_File,mode='w+') as _13_File,open(file=ned19_File,mode='w+') as ned19_File:

        for data in file.readlines():
            if("one_meter" in data):
                #writing the one_meter file
                one_meter_File.write(data)
            elif("_13" in data):
                # writing the _13 file
                _13_File.write(data)
            elif("ned19" in data):
                # writing the ned19 file
                ned19_File.write(data)


        one_meter_File.close()
        _13_File.close()
        ned19_File.close()
        file.close()
    print("File Write Done")

except FileNotFoundError:
    print("File Not Found")

#spark-submit --master local[*] project.py

I am having this error

with open(file=imageFile,mode="r+") as file, open(file=one_meter_File,mode='w+') as one_meter_File,open(file=_13_File,mode='w+') as _13_File,open(file=ned19_File,mode='w+') as ned19_File:
TypeError: Required argument 'name' (pos 1) not found

Can somebody please help. Thanks in advance.

1 Answers

As you can see here the built-in function open doesn't take the file as a keyword argument, it needs to be set directly, your error says that the required argument "name" isn't found, it's because you need to set the name of your files directly and not as a keyword argument

i.e : open(one_meter_File, 'w+')

Edit in case of people reading this : As stated in the comments this error only happens on python 2 because python 3 handles kwargs as normal args if the key name is right. So proofcheck your python installation, or correct it to python 2 needs, here are the python 2 docs for open

Related