Invalid Argument error while writing to a file in Python - python-3.x

I have been using the following function to write files and it has always been working.
However, it throws an invalid argument error after successful for the first few right now. Very confused. There is nothing special about the file name. I'm using Python 3.7. Thank you.
def write_output(filename, cleaned_text, flag = 'w+'):
with open(filename, flag, encoding='utf-8', errors='ignore') as outfile:
outfile.write(cleaned_text)
with open(filename, flag, encoding='utf-8', errors='ignore') as outfile:
OSError: [Errno 22] Invalid argument:
'D:\\EDGAR_DATA_SEAGATE\\10K_filings_HTM_clean\\2016\\8818_Avery Dennison Corp_2016-02-24_10-
K_0001047469-16-010376_clean.txt'
Process finished with exit code 1

Related

Python argparse opening multiple files

i'm trying to access multiple .txt files with argparse and i've stumbled across a problem which i can't put my head around.
parser = argparse.ArgumentParser()
parser.add_argument('filename', nargs='+'. type=argparse.FileType('r'))
args = parser.parse_args()
with open(args.filename, 'r') as files:
#do stuff to files
and i'm trying to access with
EDIT:
python3 script.py file1.txt file2.txt
But i'm getting an error as such:
Traceback (most recent call last):
File "script.py", line 34 in <module>
with open(args.filename, 'r') as files:
TypeError: expected str, bytes or os.PathLike objects, not list
I somewhat know what that means, but i just can't put my finger on what to do next.
From documentation nargs:
'+'. Just like '*', all command-line args present are gathered into a list. Additionally, an error message will be generated if there wasn’t at least one command-line argument present.
So, If you want to open multiple files, u have to iter through your list with args. For example:
parser = argparse.ArgumentParser()
parser.add_argument('filename', nargs='+')
args = parser.parse_args()
for file_name in args.filename:
with open(file_name, 'r') as files:
<do your code here>
# a = files.read()
# print(a)

How to fix "no such file found error" in python

I'm trying to perform feature extraction from bytefiles.
While opening the file through
with open('byteFiles/'+file,"r") as fp:
...
I am getting an error "Nosuchfilefound". I have checked that the file exists and even tried putting r before bytefiles
with open(r'byteFiles/'+file,"r") as fp:
...
but still I am unable to fix it.
with open('byteFiles/'+file,"r") as fp:
lines=""
for line in fp:
a=line.rstrip().split(" ")[1:]
b=' '.join(a)
b=b+"\n"
text_file.write(b)
fp.close()
os.remove('byteFiles/'+file)
text_file.close()
I am getting the error:
FileNotFoundError: [Errno 2] No such file or directory: 'byteFiles/01azqd4InC7m9JpocGv5'

Python 3 and TypeError: a bytes-like object is required, not 'str' error

Trying to run this code in python 3
def write_file(file_name, data):
if file_name is None:
print ('file_name cannot be none\n')
sys.exit(0)
with open(file_name, 'ab') as fp:
if fp:
fp.seek(0, os.SEEK_END)
fp.write(data)
fp.close()
else:
print ('%s write fail\n' % file_name)
and seeing this error:
TypeError: a bytes-like object is required, not 'str'
I am not sure how to define data as bytes?
Consider changing the file mode from
with open(file_name, 'ab') as fp:
To
with open(file_name, 'a') as fp:
Since 'ab' mode tries to open the file in binary format.
According to https://www.tutorialspoint.com/python3/python_files_io.htm
format: ab
Opens a file for appending in binary format. The file pointer is at
the end of the file if the file exists. That is, the file is in the
append mode. If the file does not exist, it creates a new file for
writing.

Getting error while coping a file from one folder into another in python

i am trying to copy a file from one folder into another. i am passing the file name as an argument which i want to copy.
des_folder = 'test_corpus'
if 3 != len(sys.argv):
print("\nUsage: %s category_name\n" % sys.argv[0])
sys.exit(1)
corpus_root = os.path.abspath('./test_data_set/' + sys.argv[1] +sys.argv[2])
filename = sys.argv[2]
test =shutil.copy(filename,des_folder)
in the command prompt i am giving the argument " test.py test sport 39280377.txt " but i am getting the error:
File "/usr/lib/python3.5/shutil.py", line 235, in copy
copyfile(src, dst, follow_symlinks=follow_symlinks)
File "/usr/lib/python3.5/shutil.py", line 114, in copyfile
with open(src, 'rb') as fsrc:
FileNotFoundError: [Errno 2] No such file or directory: 'sport-39280377.txt'
if anyone know how to slove it please guide me.
This might be help you
First you have to remove space in your file name
For [Errno 2] : maybe you should put the specific dictionary of your file - C:/sport39280377.txt
Thats what i know, sorry if i wrong

EOF error when using "rb"

here is my code:
authors_file_handler = open(authors_file,'r')
authors = pickle.load(authors_file_handler)
authors_file_handler.close()
authors_file is a .pkl file and I have confirmed info that it is not empty.
On using 'rb' or 'r+b' in place of 'r', I get an error:
File "../tools\email_preprocess.py", line 33, in preprocess
authors = pickle.load(authors_file_handler)
EOFError: Ran out of input
PLease advise on how to handle this error.

Resources