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).
Related
I was wondering if it's possible for me to include another python file in the actual one?
I'm running an app.py from the terminal with Flask and I want, when I click on submit that python run 5 lines of code, like nothing. But when I do the variable from the first python file can't be read on the second and even if I put the same variable into the second python file then it still doesn't work.
I want to run a "for", this is the code
for line in fin:
line = re.sub('APP:NAME', name, line)
line = re.sub('APP:USERNAME', username, line)
line = re.sub('APP:TEXT', tweet, line)
fout.write(line)
I checked all the forums and I didn't find the solution.
Thank you
Method 1
I think your need is to include a python file to another python file in a same directory.
You can import a python file as shown below
import filename
here, the filename is the filename of the file in the same location without file extension.
and you can call functions inside that file by calling like this.
filename.function()
Method 2
if you which to rename that filename and use it as something else:
import filename as fn
here, the filename is the filename of the file in the same location without file extension.
and you can call functions inside that file by calling like this.
fn.functionName()
Method 3
or you can simply use
from filename import *
and call the functions in that file as normal functions in the current python file like
functionname()
but Better method is method 1.
I have a program that asks the user to enter a directory containing n number of .txt files (texts). I want to open and read all the texts files of this directory entered by the user and, after that, put all the texts together in only one file sequentially...with the code below a get a error message below, which does not make sense because the file "Chapter22.txt" is in the Folder. Can anyone help understand what is going on?
Traceback (most recent call last):
File "/Users/nataliaresende/Dropbox/PYTHON/join_files.py", line 27,
in <module>
join_texts()
File "/Users/nataliaresende/Dropbox/PYTHON/join_files.py", line 13,
in join_texts
with open(file) as b:
FileNotFoundError: [Errno 2] No such file or directory: 'Chapter22.txt'
I've tried to loop in the entered directory opening and reading each of the files and appending them to a list. After that, use the ' '. join method to convert the texts appended to the list into a string. Is there an easier way to do that?
import sys
import os
from pathlib import Path
def join_texts():
files_list=[]
files_directory = Path(input('Enter the path of the files: '))
for file in os.listdir(files_directory):
with open(file) as b:
f=b.read()
files_list.append(f)
joined_files=' '.join(files_list)
print(joined_files)
join_texts()
The expected result would be a file containing all the texts from this directory together. Can anyone help me with that?
I am getting this error when trying to print the contents of a CSV file in Python.
Traceback (most recent call last):
File "/Users/cassandracampbell/Library/Preferences/PyCharmCE2018.2/scratches/Player.py", line 5, in
with open('player.csv') as csvfile:
FileNotFoundError: [Errno 2] No such file or directory: 'player.csv'
Get the exact file path to the csv, if you are on a windows get the entire folder path and then the name, and then do:
with open(r'C:\users\path\players.csv') as csvfile:
If you're using a windows and the exact path, easiest to put the r before the path like I did because it is a literal which will allow the string to be interpreted and the path to be found.
You must put player.csv to the same location with your script Player.py
Example like your code, both files should be here: /Users/cassandracampbell/Library/Preferences/PyCharmCE2018.2/scratches/
Or you can put the specific directory of player.csv,
Ex:
with open("/Users/cassandracampbell/Library/Preferences/PyCharmCE2018.2/scratches/player.csv") as csvfile:
...
Check that you have the file in question in same directory as your .py file you're working on. If that doesn't work you might want to use the full path to the file:
with open ('/Users/cassandracampbell/Library/Preferences/PyCharmCE2018.2/scratches/player.CSV') as csvfile:
And you should try to check the name of the file too should be case sensitive otherwise, let's say you have Player.csv then don't try to open player.csv all lower case won't work!
Plus I don't know what you're trying to do with your CSV file, just print the raw content? You might like using pandas.
I wrote a python script for a friend that:
takes a CSV of photos she's been cataloging that has the name of the photos in an ordered list
finds the image files on the filesystem
matches the files in the csv with files on the system
copies the images on the filesystem to a folder with a figure name in the order the files appear in the CSV
So essentially, it does:
INPUT: myphoto1.tiff, mypainting.jpeg, myphoto9.jpg, orderedlist.csv
OUTPUT: fig001.jpg, fig002.tiff, fig003.jpeg
This code is going to run on a mac. This works fine except we ran into an issue where some of the files (all by the same photographer) have 1 bracket in them, e.g.
myphoto[fromitaly.jpg
This seems to break my regular expression search:
The relevant code:
orderedpaths = [path for item in target for path in filenames if re.search(item, path)]
Where filenames is a list of the photo files on the system and target is the list from the CSV. This code is supposed to match the CSV file name (and it's subsequent order in the list) to the filename to give an ordered list of the filenames on the system.
The error:
Traceback (most recent call last):
File "renameimages.py", line 43, in <module>
orderedpaths = [path for item in target for path in filenames if re.search(item, path)]
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/re.py", line 142, in search
return _compile(pattern, flags).search(string)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/re.py", line 244, in _compile
raise error, v # invalid expression
sre_constants.error: unexpected end of regular expression
I tried or considered:
Changing the filenames/csv, but this isn't scalable and ideally her
department will be using this script more in the future
Investigating treating the files as "raw" -- but it didn't seem like
that was possible for input from CSV
Deleting the [ character from the input, but the problem is that
then the input won't match the actual files on the system.
I suppose I should mention I only suspect this was the issue: by printing out the progress of the code, it appears as if the code gets to the CSV item with the bracket and errors.
The relevant code is the part where you buld a regular expression using a user input, without sanitizing it. You should not do that.
I believe you don't need to use RE at all. you can find matching string using if item in path or path.endswith(item) or something like that.
The best option is to use your library:
from os.path import basename
orderedpaths = [ ... if basename(path) == item]
If you insist on using REs, you should escape your input using re.escape():
orderedpaths = [path for item in target for path in filenames
if re.search(re.escape(item), path)]
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)