What I am wanting to do is use the name of the current file I have that is from a generator and use the first section of the name + append .csv
The buffered stream looks like this
<_io.BufferedReader name='data/20160107W FM0.xml'>
I am having an issue with this code:
for file_to_read in roots:
print(file_to_read)
base = os.path.basename(file_to_read)
print(base)
name_to_write = os.path.splitext(file_to_read)[0]
outname = str(name_to_write[0]) + ".csv"
outdir = "output"
with open(os.path.join(outdir, outname), 'w', newline='') as csvf:
I receive this error which I believe means I am trying to split the stream rather than the name attribute of the buffered stream. Which leads me to this error.
$ python race.py data/ -e .xml
<_io.BufferedReader name='data/20160107W FM0.xml'>
Traceback (most recent call last):
File "race.py", line 106, in <module>
data_attr(rootObs)
File "race.py", line 40, in data_attr
base = os.path.basename(file_to_read)
File "C:\Users\Sayth\Anaconda3\lib\ntpath.py", line 232, in basename
return split(p)[1]
File "C:\Users\Sayth\Anaconda3\lib\ntpath.py", line 204, in split
d, p = splitdrive(p)
File "C:\Users\Sayth\Anaconda3\lib\ntpath.py", line 139, in splitdrive
if len(p) >= 2:
TypeError: object of type '_io.BufferedReader' has no len()
My expected output is:
20160107W FM0.csv
for a file you are reading/writing this works:
filepath = '../data/test.txt'
with open(filepath, 'w') as file:
print(file) # -> <_io.TextIOWrapper name='../data/test.txt' mode='w' encoding='UTF-8'>
print(file.name) # -> ../data/test.txt
but the type here is <_io.TextIOWrapper name='../data/test.txt' mode='w' encoding='UTF-8'> so i am not entirely sure how you open your file or get a _io.BufferedReader.
i assume they are both derived from io.FileIO and should therefore have a .name attribute.
thanks to Ashwini Chaudhary's comment, i can recreate your exact situation:
from io import BufferedReader
filepath = '../data/test.txt'
with BufferedReader(open(filepath, 'r')) as file:
print(file) # -> <_io.BufferedReader name='../data/test.txt'>
print(file.name) # -> ../data/test.txt
Related
import os
import eyed3
def files(path):
for file in os.listdir(path):
if os.path.isfile(os.path.join(path, file)):
yield file
def title_alteration(music_artist_string):
music_artist_string = music_artist_string.replace(';', ' feat. ', 1)
semicolon_count = music_artist_string.count(';')
music_artist_string = music_artist_string.replace(';', ', ', semicolon_count-1)
music_artist_string = music_artist_string.replace(';', ' & ')
return music_artist_string
def main():
audio_files = eyed3.load(files('D:\\iTunes Music\\iTunes\\iTunes Media\\Music'))
title_alteration(audio_files.tag.artist)
if __name__ == '__main__':
main()
Can I get some help debugging this, I got it down to three distinct functions with some help from my last post, now I just need to know why is this getting errors when I attempt to run it on this directory on my pc.
I'm getting these errors (TLDR; It doesnt like Line 20 [the audio_files line]):
Traceback (most recent call last):
File "D:/Pycharm Projects/Music Alterations v2.py", line 25, in <module>
main()
File "D:/Pycharm Projects/Music Alterations v2.py", line 20, in main
audio_files = eyed3.load(files('D:\\iTunes Music\\iTunes\\iTunes Media\\Music'))
File "C:\Users\cLappy\AppData\Local\Programs\Python\Python38\lib\site-packages\eyed3\core.py", line 74, in load
path = pathlib.Path(path)
File "C:\Users\cLappy\AppData\Local\Programs\Python\Python38\lib\pathlib.py", line 1038, in __new__
self = cls._from_parts(args, init=False)
File "C:\Users\cLappy\AppData\Local\Programs\Python\Python38\lib\pathlib.py", line 679, in _from_parts
drv, root, parts = self._parse_args(args)
File "C:\Users\cLappy\AppData\Local\Programs\Python\Python38\lib\pathlib.py", line 663, in _parse_args
a = os.fspath(a)
TypeError: expected str, bytes or os.PathLike object, not generator
Process finished with exit code 1
Your files function is a generator, to be used like an iterator. The eyed3.load function doesn't want a generator or an iterator. It wants a pathname or something like one. Passing a generator where a pathname is expected does not magically cause iteration over all the values the generator would generate. It would work better just to make a list of all the pathnames of interest and then iterate over that list, calling eyed3.load for each pathname.
Traceback (most recent call last):
File "C:\Users\me\folder\project.py", line 999, in <module>
load_data()
File "C:\Users\me\folder\project.py", line 124, in load_data
globals()[var_name] = pickle.(f)
EOFError: Ran out of input
I get this error when trying to unpickle a file, even though the file is non-empty. I've tried opening the file and printing its value and it is non-empty, but the unpickler returns this result still.
Anyone know why this may be happening?
The code here is as follows:
files_to_load = ['var1','var2',...]
def load_data():
for var_name in files_to_load:
path = '{}.txt'.format(var_name)
if os.path.exists(path):
with open(path, 'rb') as f:
globals()[var_name] = pickle.Unpickler(f).load()
else: globals()[var_name] = {}
I would like to save a pandas.DataFrame object into a .csv file (using DataFrame.to_csv()). To make it simple, here is the situation; my project 'Algos' contains the folder 'Plip' with a .txt file called 'plop.txt':
1,2
3,4
5,6
My script is here:
import numpy as np
import pandas as pa
# opening the file
dossier = "Plip"
fichier = "plop.txt"
fichier = open(dossier + "/" + fichier)
data = fichier.readlines()
# creating the data frame
Pair = []
Impair = []
for m in data:
impair = int(m[0:1])
pair = int(m[2:3])
Impair.append(impair)
Pair.append(pair)
M = np.array([Impair, Pair]).transpose()
Table = pa.DataFrame(M, columns = ["Impair", "Pair"])
#creating the .csv file
Table.to_csv(dossier + fichier + ".csv")
Table has been correcty created but the script returns:
runfile('C:/Users/******/Documents/PYTHON/Algos/truc.py', wdir='C:/Users/******/Documents/PYTHON/Algos')
Traceback (most recent call last):
File "<ipython-input-110-3c7306b8d61d>", line 1, in <module>
runfile('C:/Users/******/Documents/PYTHON/Algos/truc.py', wdir='C:/Users/******/Documents/PYTHON/Algos')
File "C:\Users\******\Documents\Gratuiciels\WINPYTHON.3355\python-3.3.5\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 601, in runfile
execfile(filename, namespace)
File "C:\Users\******\Documents\Gratuiciels\WINPYTHON.3355\python-3.3.5\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 80, in execfile
exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)
File "C:/Users/******/Documents/PYTHON/Algos/truc.py", line 30, in <module>
Table.to_csv(dossier + fichier + ".csv")
TypeError: Can't convert '_io.TextIOWrapper' object to str implicitly
I'm quite new on Python so could you be as precise as possible on your answer please ?
Thank you in advance
Seems like you reassigned fichier to a file object on L7, use a new variable instead!
I downloaded Brive which downloads your Google Docs using the Drive API. I'm running into issues with the filename not saving if it has slashes and crashes the application. How can I modify the model.py file to rewrite / as _?
model.py
brive.py
I think I just need to rewrite the "file_name" or "path" on line 74.
backend.py:
def save(self, user, document):
self._mkdir(user.login)
prefix = self._root_dir + user.login + os.sep
for file_name, content in document.contents.items():
path = prefix + file_name
Log.debug(u'Writing {}\'s {} to {}'.format(
user.login, document.title, path
))
f = open(path, 'w')
f.write(content)
f.close()
This is the error:
[ 2013-01-17 T 06:17:08 Z ] Saving coral.lopez's doc "Lunchbox Monster High 4/7/12" (id: 1GyiuKFZeargO8KfzKS5H9V3PVbgTJufw2PwLaILzRVw)
[ 2013-01-17 T 06:17:08 Z ] Unexpected shutdown, deleting /home/davidneudorfer/google_docs_backup/2013-01-17T061021Z/ folder
### Unexpected error when saving coral.lopez's documents (doc id: 1GyiuKFZeargO8KfzKS5H9V3PVbgTJufw2PwLaILzRVw) ###
Traceback (most recent call last):
File "brive.py", line 114, in <module>
main()
File "brive.py", line 92, in main
user.save_documents(backend)
File "/home/davidneudorfer/Brive/model.py", line 79, in save_documents
self._save_single_document(backend, document)
File "/home/davidneudorfer/Brive/model.py", line 105, in _save_single_document
backend.save(self, document)
File "/home/davidneudorfer/Brive/backend.py", line 78, in save
f = open(path, 'w')
IOError: [Errno 2] No such file or directory: u'/home/davidneudorfer/google_docs_backup/2013-01-17T061021Z/coral.lopez/Lunchbox Monster High 4/7/12_1GyiuKFZeargO8KfzKS5H9V3PVbgTJufw2PwLaILzRVw.odt'
You could use the_str.replace('/', '_') to turn the path with '/'s in it into one with '_' in it.
try.py:
import pickle
f=open('abc.dat','w')
x=[320,315,316]
y=pickle.load(f)
f.close()
f=open('abc.dat','w')
for i in x:
y.append(i)
pickle.dump(y,f)
f.close()
use.py
import pickle
import os
os.system('try.py')
f=open('abc.dat', 'r')
print "abc.dat = "
x=pickle.load(f)
print x
print "end of abc.dat"
f.close();
y=x[:]
for z in x:
y.remove(z)
print "removing " + str(z)
print str(y) + " and " + str(x)
f=open('abc.dat', 'w')
pickle.dump(y, f)
f.close()
error:
Traceback (most recent call last):
File "G:\parin\new start\use.py", line 7, in <module>
x=pickle.load(f)
File "C:\Python26\lib\pickle.py", line 1370, in load
return Unpickler(file).load()
File "C:\Python26\lib\pickle.py", line 858, in load
dispatch[key](self)
File "C:\Python26\lib\pickle.py", line 880, in load_eof
raise EOFError
EOFError
The error is in try.py:
f=open('abc.dat','w')
y=pickle.load(f)
Note that the 'w' mode resets the file to size 0 (i.e. deletes its content). Pass 'r' or nothing at all to open abc.dat for reading.
Example doesn't work for me. try.py fails when the file doesn't exist.
My big recommendation, though, is to look at using JSON instead of pickle, as you'll have more cross-platform flexibility and the interface is more flexible.
For example, use this to create a file of JSON lines:
import json,random
with open("data.txt","w") as f:
for i in range(0,10):
info = {"line":i,
"random":random.random()}
f.write(json.dumps(info)+"\n")
(Make info whatever you want, obviously.)
Then use this to read them:
import json
for line in open("data.txt"):
data = json.loads(line)
print("data:" + str(data))