why am i getting this IsADirectoryError: [Errno 21]? - python

download_images(path/file, dest, max_pics=400)
---------------------------------------------------------------------------
IsADirectoryError Traceback (most recent call last)
<ipython-input-16-fd768bad6ac9> in <module>()
1 #3
----> 2 download_images(path/file, dest, max_pics=400)
2 frames
/usr/lib/python3.6/pathlib.py in open(self, mode, buffering, encoding, errors, newline)
1181 self._raise_closed()
1182 return io.open(str(self), mode, buffering, encoding, errors, newline,
-> 1183 opener=self._opener)
1184
1185 def read_bytes(self):
IsADirectoryError: [Errno 21] Is a directory: 'data/marvel/thor'

If data/marvel/thor is a directory, please try follows.
import os
data_paths = [os.path.join(pth, f) for pth, dirs, files in os.walk('data/marverl/thor') for f in files]
data = [download_images(f, mmap_mode='r') for f in data_paths]
You need to provide a variable/place to store each image separately.

Related

Error trying to use AudioSegment for .wav files

I'm trying to iterate through all the .wav files in a folder "audios", but I receive the following error. I found similar questions that were solved by installing ffmpeg, but that didn't help.
FileNotFoundError Traceback (most recent call last)
<ipython-input-24-29ba732186ac> in <module>
1 for audio_file in os.listdir(base_path+"audios"):
2 # read wav audio file
----> 3 audio = AudioSegment.from_wav(audio_file)
4
5 # pass audio file, start time, end time & chunk path to create chunk
~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pydub\audio_segment.py in from_wav(cls, file, parameters)
806 #classmethod
807 def from_wav(cls, file, parameters=None):
--> 808 return cls.from_file(file, 'wav', parameters=parameters)
809
810 #classmethod
~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pydub\audio_segment.py in from_file(cls, file, format, codec, parameters, start_second, duration, **kwargs)
649 except TypeError:
650 filename = None
--> 651 file, close_file = _fd_or_path_or_tempfile(file, 'rb', tempfile=False)
652
653 if format:
~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pydub\utils.py in _fd_or_path_or_tempfile(fd, mode, tempfile)
58
59 if isinstance(fd, basestring):
---> 60 fd = open(fd, mode=mode)
61 close_fd = True
62
FileNotFoundError: [Errno 2] No such file or directory: 'name_of_file.wav'
os.listdir doesn't return the full paths of files in the directory you give it, just the names they have within that directory. You will need to prepend this directory name to the filename you pass to AudioSegment.from_wav.
Try replacing the line
audio = AudioSegment.from_wav(audio_file)
with
audio = AudioSegment.from_wav(os.path.join(base_path+"audios", audio_file))

Errno 2 No such file or directory:

I am using jupyter notebook (python 3.8 both from anaconda3) and following this post, cells 84 and 85 are resulting in the traceback and followed the advice of
FileNotFoundError Traceback (most recent call last)
<ipython-input-15-9cdebd0bb247> in <module>
2
3
----> 4 create_wordcloud(tw_list["text"].values)
<ipython-input-14-524a73dcd1e0> in create_wordcloud(text)
2
3 def create_wordcloud(text):
----> 4 mask = np.array(Image.open("cloud.png"))
5 stopwords = set(STOPWORDS)
6 wc = WordCloud(background_color="white",
~/opt/anaconda3/lib/python3.8/site-packages/PIL/Image.py in open(fp, mode, formats)
2889
2890 if filename:
-> 2891 fp = builtins.open(filename, "rb")
2892 exclusive_fp = True
2893
FileNotFoundError: [Errno 2] No such file or directory: 'cloud.png'
following this i found advice (the link evades me but its somewhere on this site to change from PIL import image to import PIL.image in cell 2 and add
from IPython.display import Image
Image(filename='cloud.png')
still resulting in a similar, but longer traceback
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
<ipython-input-16-8c5d56ae9874> in <module>
1 #Creating wordcloud for all tweets
2 from IPython.display import Image
----> 3 Image(filename='cloud.png')
4
5 create_wordcloud(tw_list["text"].values)
~/opt/anaconda3/lib/python3.8/site-packages/IPython/core/display.py in
__init__(self, data, url, filename, format, embed, width, height, retina,
unconfined, metadata)
1222 self.retina = retina
1223 self.unconfined = unconfined
-> 1224 super(Image, self).__init__(data=data, url=url, filename=filename,
1225 metadata=metadata)
1226
~/opt/anaconda3/lib/python3.8/site-packages/IPython/core/display.py in
__init__(self, data, url, filename, metadata)
628 self.metadata = {}
629
--> 630 self.reload()
631 self._check_data()
632
~/opt/anaconda3/lib/python3.8/site-packages/IPython/core/display.py in
reload(self)
1254 """Reload the raw data from file or URL."""
1255 if self.embed:
-> 1256 super(Image,self).reload()
1257 if self.retina:
1258 self._retina_shape()
~/opt/anaconda3/lib/python3.8/site-packages/IPython/core/display.py in
reload(self)
653 """Reload the raw data from file or URL."""
654 if self.filename is not None:
--> 655 with open(self.filename, self._read_flags) as f:
656 self.data = f.read()
657 elif self.url is not None:
FileNotFoundError: [Errno 2] No such file or directory: 'cloud.png'
which evidently is not the right solution, I am a little out of my depth here and grateful for any help
That means the file does not exist in the directory it is called. You must download their 'cloud.png' and put it in the same file as the jupyter notebook file.
https://github.com/ChilesheChanda/TwitterSentimentAnalysis/blob/master/cloud.png

QST: error while using pickle to load the files

I am getting the below error while using pickle to load the files on kaggle.
It has worked for everyone, but it is not working for me. The file path is correct.
Thank you for your help.
My code:
%%time
import pickle
#using one of the validation sets composed by tito
cv2_train = pd.read_pickle("../input/riiid-cross-validation-files/cv2_train.pickle")['row_id']
cv2_valid = pd.read_pickle("../input/riiid-cross-validation-files/cv2_valid.pickle")['row_id']
Error:
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
<timed exec> in <module>
/opt/conda/lib/python3.7/site-packages/pandas/io/pickle.py in read_pickle(filepath_or_buffer, compression)
167 if not isinstance(fp_or_buf, str) and compression == "infer":
168 compression = None
--> 169 f, fh = get_handle(fp_or_buf, "rb", compression=compression, is_text=False)
170
171 # 1) try standard library Pickle
/opt/conda/lib/python3.7/site-packages/pandas/io/common.py in get_handle(path_or_buf, mode, encoding, compression, memory_map, is_text, errors)
497 else:
498 # Binary mode
--> 499 f = open(path_or_buf, mode)
500 handles.append(f)
501
FileNotFoundError: [Errno 2] No such file or directory: '../input/riiid-cross-validation-files/cv2_train.pickle'

Errno 13 while attempting to plot an HMM with pomegranate

I'm trying to plot an HMM model, implemented through the usage of the Pomegranate library.
Calling the plot method with model.plot() invokes the following error:
PermissionError Traceback (most recent call last)
<ipython-input-29-405034733d4b> in <module>
3 #unique_data = [list(x) for x in set(tuple(x) for x in result)]
4 #unique_data
----> 5 model.plot()
D:\ProgramData\Anaconda3\lib\site-packages\pomegranate\hmm.pyx in pomegranate.hmm.HiddenMarkovModel.plot()
D:\ProgramData\Anaconda3\lib\site-packages\pomegranate\hmm.pyx in pomegranate.hmm.HiddenMarkovModel.plot()
D:\ProgramData\Anaconda3\lib\site-packages\pygraphviz\agraph.py in draw(self, path, format, prog, args)
1516
1517 if path is not None:
-> 1518 fh = self._get_fh(path, 'w+b')
1519 fh.write(data)
1520 if is_string_like(path):
D:\ProgramData\Anaconda3\lib\site-packages\pygraphviz\agraph.py in _get_fh(self, path, mode)
1545 fh = os.popen("bzcat " + path) # probably not portable
1546 else:
-> 1547 fh = open(path, mode=mode)
1548 elif hasattr(path, 'write'):
1549 # Note, mode of file handle is unchanged.
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\renda\\AppData\\Local\\Temp\\tmp672ou8lg'
I tried to change the R/W permission on the folder, but it didn't work. Any suggestion?
I'm working on Windows 10, using Anaconda3 environment.

Iterate through all files in a directory and find and replace text - Python

Baby brand new. This was Frankenstein'ed together from a few similar topics, none of which seemed to cover the necessary step of nesting a find and replace inside a file loop.
I am attempting to iterate through every file in a folder (not recursively, I only have one folder level) of a specific type (listed here as a '.LIC') and replace a short bit of text. The following is as close as I could come:
import glob, os, fileinput
from glob import glob
root_dir = r"myPath"
os.chdir(root_dir)
for file in glob, glob('*.LIC'):
filename = str(file)
with fileinput.FileInput(filename, inplace=True, backup='.bak') as file:
for line in file:
print(line.replace('findText', 'replaceText'), end='')
As you can imagine this went swimmingly. The error code is placed below.
OSError Traceback (most recent call last)
<ipython-input-61-e2fd0e9a5df9> in <module>()
6 filename = str(file)
7 with fileinput.FileInput(filename, inplace=True, backup='.bak') as file:
----> 8 for line in file:
9 print(line.replace('findText', 'replaceText'), end='')
10
C:\Users\Me\Anaconda3\lib\fileinput.py in __next__(self)
246 def __next__(self):
247 while True:
--> 248 line = self._readline()
249 if line:
250 self._filelineno += 1
C:\Users\Me\Anaconda3\lib\fileinput.py in _readline(self)
333 pass
334 # The next few lines may raise OSError
--> 335 os.rename(self._filename, self._backupfilename)
336 self._file = open(self._backupfilename, self._mode)
337 try:
OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: '<function glob at 0x00000000013D3400>' -> '<function glob at 0x00000000013D3400>.bak'
I think my problem is nesting a reference to 'file', but I am unsure how to resolve this.
Thank you for the help in advance.
You should loop over the result of glob and not a tuple with the function object glob:
for filename in glob('*.LIC'):
with fileinput.FileInput(filename, inplace=True, backup='.bak') as file:
for line in file:
print(line.replace('findText', 'replaceText'), end='')

Categories