How to fix "no such file found error" in python - python-3.x

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'

Related

Python3 gzip create new file which did not exist yet

I struggle with python3.9, maybe I'm just blind, so give me a hint please.
Accoring to documententation the following code should create a ".gz"-file.
import gzip
content = b"Lots of content here"
with gzip.open('/home/joe/file.txt.gz', 'wb') as f:
f.write(content)
Is it correct to assume that the file file.txt.gz does not exist before that operation and it will be created during that operation?
My code looks like:
import gzip
...
class FileHandler:
...
def archive(self):
self.content = 'Hello compressed World'
zipFile = '/home/user/archive/test.gz'
print(f'{zipFile}')
with gzip.open(zipFile, 'wt') as f:
f.write(self.content)
...
if __name__ == '__main__':
fp = FileHandler()
fp.archive()
I get the following exception:
Traceback (most recent call last):
File "/home/user/filehandling.py", line 55, in <module>
fp.archive()
File "/home/user/filehandling.py", line 46, in archive
with gzip.open(zipFile, 'wt') as f:
File "/usr/lib/python3.9/gzip.py", line 58, in open
binary_file = GzipFile(filename, gz_mode, compresslevel)
File "/usr/lib/python3.9/gzip.py", line 173, in __init__
fileobj = self.myfileobj = builtins.open(filename, mode or 'rb')
FileNotFoundError: [Errno 2] No such file or directory: '/home/user/archive/test.gz'
Obvisiously the file does not exist. Why is it not created as mentioned in the documentation?
So what I am doing wrong here?
---- SOLVED ----
#Czaporka is right, I was missing a part of the path

Invalid Argument error while writing to a file in Python

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

paramiko sftp - when trying to download a file, I get OSError File path illegal

Initializing paramiko sftp client:
t = paramiko.Transport((self.sftp_server, 7790))
t.connect(None,self.sftp_user, self.sftp_pw)
sftp = paramiko.SFTPClient.from_transport(t)
listing the files:
dirlist = sftp.listdir(".")
print("Dirlist: %s" % dirlist)
files = sftp.listdir(path=self.location)
print(files)
gives all the files correctly.
but when reading a file form the list
with sftp.open('/VA_EBAC_UP/EBAC_Article_KPI_Report.txt', "r") as f:
data = f.read()
I get an error:
Caught exception: : File path
[EBAC_Article_KPI_Report.txt] illegal.
full stack trace:
Traceback (most recent call last):
File "get_sftp_file_yohan.py", line 234, in get_ftp_files
with sftp.open('/VA_EBAC_UP/EBAC_Article_KPI_Report.txt', "r") as f:
File "/lib/python3.6/site-packages/paramiko/sftp_client.py", line 372, in open
t, msg = self._request(CMD_OPEN, filename, imode, attrblock)
File "/lib/python3.6/site-packages/paramiko/sftp_client.py", line 813, in _request
return self._read_response(num)
File "/lib/python3.6/site-packages/paramiko/sftp_client.py", line 865, in _read_response
self._convert_status(msg)
File "/lib/python3.6/site-packages/paramiko/sftp_client.py", line 898, in _convert_status
raise IOError(text)
OSError: File path [EBAC_Article_KPI_Report.txt] illegal.
this was a Permissions issue. The SFTP server only allowed uploads and I was trying to download from the same server. Maybe this will help someone else - but wish that the error message was verbose enough to highlight permission issue.
**d-wx------** 1 0 0 4096 05 Nov 00:00 VA_EBAC_UP
reading any file from the above folder gives:
Caught exception: : File path [] illegal.

FileNotFoundError: [Errno 2] No such file or directory: '2MCREF~E.JPG'

I'm trying to open an image that resides at a different location than my script
Code:
import os
from PIL import Image
folder = '/Users/abc'
if not os.listdir(folder):
print('Folder not found')
else:
print('"{}" found'.format(folder))
for file in os.listdir(folder):
print(file)
data = Image.open(file,'r')
print('Done')
Error:
"/Users/abc" found
2MCREF~E.JPG
Traceback (most recent call last):
File "img_to_s3bucket.py", line 25, in <module>
data = Image.open(file,'r')
File "/Users/AjayB/anaconda3/envs/MyDjangoEnv/lib/python3.6/site-packages/PIL/Image.py", line 2770, in open
fp = builtins.open(filename, "rb")
FileNotFoundError: [Errno 2] No such file or directory: '2MCREF~E.JPG'
How to tackle this?
This could be because your working directory and the location of the file are not same.
You could do this by specifying the full file path in the below command:
data = Image.open(file,'r')
you can do this by:
data = Image.open(os.path.join(folder, file),'r'))

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

Resources