I'm trying to open a number of files using glob and feed them through a series of functions. Some of my files are gziped some are bz2 and some are plain text. I used fileinput typically but can't figure out the syntax to have it take in compressed files. Based on this Python Fileinput Doc it should be something like:
openhook=fileinput.hook_compressed
My code looks like:
import fileinput
import glob
filestobeanalyzed = glob.glob('./files/*')
for fileName in filestobeanalyzed:
inputfilename = fileName
for line in fileinput.input([inputfilename, openhook=fileinput.hook_compressed]):
#do stuff
I get an invalid syntax on the fileinput line at the = sign.
Any suggestions?
You want
for line in fileinput.input(inputfilename, openhook=fileinput.hook_compressed):
#do stuff
(I removed the square brackets). You were trying to do an assignment in a list constructor. e.g.
my_list=["foo",bar="baz"] #this doesn't work (SyntaxError)
You probably got the idea from the python documentation which uses [ and ] to indicate optional arguments to functions.
This is just an aside -- often there is more information in the traceback which can help pin down the problem than just the type of error and the line number. (read: When you have a traceback, it's generally appreciated if you paste the whole thing so we can see it)
Related
I am very fresh in Python. I would like to read JSON files in Python, but I did not get what are the problems. Please see the image.
You have to specify a mode to the open() function. In this case I think you're trying to read the file, so your mode would be "r". Your code should be:
with open(r'path/to/read/','r') as file:
data = json.load(file)
Your code should run now.
Your path should not contain spaces. Please modify the file path.
Generally speaking, the file path is best to be in full English with no spaces and no special characters.
import sys
import os
import json
def JsonRead(str):
with open(str, encoding='utf-8') as f:
data = json.load(f)
return data
new_Data = JsonRead(filePath)
Then import JsonRead in project
I'm trying, just for fun, to understand if I can extract the full path of my file while using the with statement (python 3.8)
I have this simple code:
with open('tmp.txt', 'r') as file:
print(os.path.basename(file))
But I keep getting an error that it's not a suitable type format.
I've been trying also with the relpath, abspath, and so on.
It says that the input should be a string, but even after casting it into string, I'm getting something that I can't manipulate.
Perhaps there isn't an actual way to extract that full path name, but I think there is. I just can't find it, yet.
You could try:
import os
with open("tmp.txt", "r") as file_handle:
print(os.path.abspath(file_handle.name))
The functions in os.path accept strings or path-like objects. You are attempting to pass in a file instead. There are lots of reasons the types aren't interchangable.
Since you opened the file for text reading, file is an instance of io.TextIOWrapper. This class is just an interface that provides text encoding and decoding for some underlying data. It is not associated with a path in general: the underlying stream can be a file on disk, but also a pipe, a network socket, or an in-memory buffer (like io.StringIO). None of the latter are associated with a path or filename in the way that you are thinking, even though you would interface with them as through normal file objects.
If your file-like is an instance of io.FileIO, it will have a name attribute to keep track of this information for you. Other sources of data will not. Since the example in your question uses FileIO, you can do
with open('tmp.txt', 'r') as file:
print(os.path.abspath(file.name))
The full file path is given by os.path.abspath.
That being said, since file objects don't generally care about file names, it is probably better for you to keep track of that info yourself, in case one day you decide to use something else as input. Python 3.8+ allows you to do this without changing your line count using the walrus operator:
with open((filename := 'tmp.txt'), 'r') as file:
print(os.path.abspath(filename))
I have a very simple python script that should scan a text file, which contains lines formatted as id='value' and put them into a dict. the python module is called chval.py and the input file is in.txt. here's the code:
import os,sys
from os import *
from sys import *
vals = {}
f = open(sys.argv[1], 'r')
for line in val_f:
t = line.split('=')
t[1].strip('\'')
vals.append(t[0], t[1])
print vals
f.close()
when i try to run it i get:
Traceback (most recent call last):
File "chval.py", line 9, in ?
f = open(sys.argv[1], 'r') TypeError: an integer is required
I'm using python 2.4... because i've been challenged to not use anything newer, is there something about open() that I don't know about? Why does it want an integer?
anything after that line is untested. in short: why is it giving me the error and how do i fix it?
Because you did from os import *, you are (accidenally) using os.open, which indeed requires an integer flag instead of a textual "r" or "w". Take out that line and you'll get past that error.
Don't do import * from wherever without a good reason (and there aren't many).
Your code is picking up the os.open() function instead of the built-in open() function. If you really want to use os.open(), do import os then call os.open(....). Whichever open you want to call, read the documentation about what arguments it requires.
Also of note is that starting with Python 2.6 the built-in function open() is now an alias for the io.open() function. It was even considered removing the built-in open() in Python 3 and requiring the usage of io.open, in order to avoid accidental namespace collisions resulting from things such as "from blah import *". In Python 2.6+ you can write (and can also consider this style to be good practice):
import io
filehandle = io.open(sys.argv[1], 'r')
Providing these parameters resolved my issue:
with open('tomorrow.txt', mode='w', encoding='UTF-8', errors='strict', buffering=1) as file:
file.write(result)
From http://www.tutorialspoint.com/python/os_open.htm you could also keep your import and use
file = os.open( "foo.txt", mode )
and the mode could be :
os.O_RDONLY: open for reading only
os.O_WRONLY: open for writing only
os.O_RDWR : open for reading and writing
os.O_NONBLOCK: do not block on open
os.O_APPEND: append on each write
os.O_CREAT: create file if it does not exist
os.O_TRUNC: truncate size to 0
os.O_EXCL: error if create and file exists
os.O_SHLOCK: atomically obtain a shared lock
os.O_EXLOCK: atomically obtain an exclusive lock
os.O_DIRECT: eliminate or reduce cache effects
os.O_FSYNC : synchronous writes
os.O_NOFOLLOW: do not follow symlinks
that's because you should do:
open(sys.argv[2], "w", encoding="utf-8")
or
open(sys.argv[2], "w")
you have from os import * I also got the same error, remove that line and change it to import os and behind the os lib functions, add os.[function]
I am trying to read in an excel file using xlrd and use tkinter to find the file. I am having trouble with the \ operator. If I would hard code the file path I could use the raw string command, r'filepath'. Is there something similar I could use, like, r(filepath) to convert the string to a raw string? My code and the error is listed below,
import xlrd
from tkinter import Tk, filedialog, messagebox
application_window = Tk()
application_window.withdraw()
messagebox.showinfo("Information", "Select the Layout")
path = filedialog.askopenfilenames(title="Layout")
Layout = xlrd.open_workbook(path)
Error:
File "C:\Program Files\Anaconda3\lib\site-packages\xlrd__init__.py", line 395, in open_workbook with open(filename, "rb") as f:
FileNotFoundError: [Errno 2] No such file or directory: "('filepath',)"
There is no such thing as a raw-string within a running Python interpreter. raw-strings are only useful for written Python-Code to make it easier for programmers to enter otherwise escaped characters - most notably back-slashes that are also used in regular expressions as escape characters themselves.
Your problem is different: you think you get a path from askopenfilename. But what you get is a tuple with a number of paths, in your case just one.
So just use tkinter.filedialog.askopenfilename without the trailing s to get just one.
I am trying to automate some plotting using python and fortran together.
I am very close to getting it to work, but I'm having problems getting the result from a glob search to feed into my python function.
I have a .py script that says
import glob
run=glob.glob('JUN*.aijE*.nc')
from plot_check import plot_check
plot_check(run)
But I am getting this error
plot_check(run)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "plot_check.py", line 7, in plot_check
ncfile=Dataset(run,'r')
File "netCDF4.pyx", line 1328, in netCDF4.Dataset.__init__ (netCDF4.c:6336)
RuntimeError: No such file or directory
I checked that the glob is doing its job and it is, but I think it's the format of my variable "run" that's screwing me up.
In python:
>>run
>>['JUN3103.aijE01Ccek0kA.nc']
>>type(run)
<type 'list'>
So my glob is finding the file name of the file I want to put into my function, but something isn't quite working when I try to input the variable "run" in to my function "plot_check".
I think it might be something to do with the format of my variable "run", but I'm not quite sure how to fix it.
Any help would be greatly appreciated!
glob.glob returns a list of all matching filenames. If you know there's always going to be exactly one file, you can just grab the first element:
filenames = glob.glob('JUN*.aijE*.nc')
plot_check(filenames[0])
Or, if it might match more than one file, then iterate over the results:
filenames = glob.glob('JUN*.aijE*.nc')
for filename in filenames:
plot_check(filename)
Perhaps Dataset expects to be passed a single string filename, rather than a list with one element?
Try using run[0] instead (though you may want to check to make sure your glob actually matches a file before you do that).