I use setuptools to distribute a python package. My directory structure is like following.
Mypackage
--setup.py
--my_package.py
Data
--some_name.log
I want users to put data files in this folder, and name can be anything with the extension .log.
log_list = []
for file in glob.glob('/home/ginger/Mypackage/Data/*.log'):
with open(file,'r') as f:
for line in f:
try:
data = p.parse(line)
except:
pass
log_list.append(data)
This code works fine. But when I try to get the absolute path from the relative path it does not read the data file. Why is this?
path = os.path.abspath("Data/*.log")
log = []
for file in glob.glob(path):
with open(file,'r') as f:
for line in f:
log.append(line)
Found the solution. Path has to be defined as following with the immediate directory.
path = os.path.abspath("Mypackage/Data/*.log")
Related
I want to read a file from 2 folders back..
with open('../../test.txt', 'r') as file:
lines = file.readlines()
file.close()
I want to read from ../../ two folders back. but not work..
How i can do that ?
Opening files in python is relative to the current working directory. This means you would have to change cd to the directory where this python file is located.
If you want a more robust solution:
To be able to run this from any directory, there is a simple trick:
import os
PATH = os.path.join(os.path.dirname(__file__), '../../test.txt')
with open(PATH, 'r') as file:
lines = file.readlines()
file.close()
I have multiple (400) json files containing a dict in a directory that I want to read and append to a list. I've tried looping over all the files in the directory like this:
path_to_jsonfiles = 'TripAdvisorHotels'
alldicts = []
for file in os.listdir(path_to_jsonfiles):
with open(file,'r') as fi:
dict = json.load(fi)
alldicts.append(dict)
I keep getting the following error:
FileNotFoundError: [Errno 2] No such file or directory
However, when I look at the files in the directory, it gives me all the right files.
for file in os.listdir(path_to_jsonfiles):
print(file)
Just opening one of them with the file name works as well.
with open('AWEO-q_GiWls5-O-PzbM.json','r') as fi:
data = json.load(fi)
Were in the loop is it going wrong?
Your code has two errors:
1.file is only the file name. You have to write full filepath (including its folder).
2.You have to use append inside the loop.
To sum up, this should work:
alldicts = []
for file in os.listdir(path_to_jsonfiles):
full_filename = "%s/%s" % (path_to_jsonfiles, file)
with open(full_filename,'r') as fi:
dict = json.load(fi)
alldicts.append(dict)
I am trying to open the file from folder and read it but it's not locating it. I am using Python3
Here is my code:
import os
import glob
prefix_path = "C:/Users/mpotd/Documents/GitHub/Python-Sample-
codes/Mayur_Python_code/Question/wx_data/"
target_path = open('MissingPrcpData.txt', 'w')
file_array = [os.path.abspath(f) for f in os.listdir(prefix_path) if
f.endswith('.txt')]
file_array.sort() # file is sorted list
for f_obj in range(len(file_array)):
file = os.path.abspath(file_array[f_obj])
join_file = os.path.join(prefix_path, file) #whole file path
for filename in file_array:
log = open(filename, 'r')#<---- Error is here
Error: FileNotFoundError: [Errno 2] No such file or directory: 'USC00110072.txt'
You are not giving the full path to a file to the open(), just its name - a relative path.
Non-absolute paths specify locations in relation to current working directory (CWD, see os.getcwd).
You would have to either os.path.join() correct directory path to it, or os.chdir() to the directory that the files reside in.
Also, remember that os.path.abspath() can't deduce the full path to a file just by it's name. It will only prefix its input with the path of the current working directory, if the given path is relative.
Looks like you are forgetting to modify the the file_array list. To fix this, change the first loop to this:
file_array = [os.path.join(prefix_path, name) for name in file_array]
Let me reiterate.
This line in your code:
file_array = [os.path.abspath(f) for f in os.listdir(prefix_path) if f.endswith('.txt')]
is wrong. It will not give you a list with correct absolute paths. What you should've done is:
import os
import glob
prefix_path = ("C:/Users/mpotd/Documents/GitHub/Python-Sample-"
"codes/Mayur_Python_code/Question/wx_data/")
target_path = open('MissingPrcpData.txt', 'w')
file_array = [f for f in os.listdir(prefix_path) if f.endswith('.txt')]
file_array.sort() # file is sorted list
file_array = [os.path.join(prefix_path, name) for name in file_array]
for filename in file_array:
log = open(filename, 'r')
You are using relative path where you should be using an absolute one. It's a good idea to use os.path to work with file paths. Easy fix for your code is:
prefix = os.path.abspath(prefix_path)
file_list = [os.path.join(prefix, f) for f in os.listdir(prefix) if f.endswith('.txt')]
Note that there are some other issues with your code:
In python you can do for thing in things. You did for thing in range(len(things)) it's much less readable and unnecessary.
You should use context managers when you open a file. Read more here.
I have a directory with lot of text files. I want to read each text file in the directory and perform some kind of search operation. I take directory name as a command line argument. The error I'm getting is IsADirectoryError. Is there anyway we can make this work without any other module?
This is my code:
a = sys.argv
files = a[1:-1]
for i in files:
print(i)
f = open(i,'rb')
for line in f:
try:
for word in line.split():
'''Rest of code here'''
try this code
def read_files_from_dir(dirname):
for _file in os.listdir(dirname):
with open(os.path.join(dirname,_file), "r") as fp:
print fp.read()
I upload file to dropbox api, but it post on dropbox all directories from my computer since root folder. I mean you have folder of your project inside folder home, than user until you go to file sours folder. If I cut that structure library can't see that it is file, not string and give mistake message.
My code is:
def upload_file(project_id, filename, dropbox_token):
dbx = dropbox.Dropbox(dropbox_token)
file_path = os.path.abspath(filename)
with open(filename, "rb") as f:
dbx.files_upload(f.read(), file_path, mute=True)
link = dbx.files_get_temporary_link(path=file_path).link
return link
It works, but I need something like:
file_path = os.path.abspath(filename)
chunks = file_path.split("/")
name, dir = chunks[-1], chunks[-2]
which gives me mistake like:
dropbox.exceptions.ApiError: ApiError('433249b1617c031b29c3a7f4f3bf3847', GetTemporaryLinkError('path', LookupError('not_found', None)))
How could I make only parent folder and filename in the path?
For example if I have
/home/user/project/file.txt
I need
/project/file.txt
you have /home/user/project/file.txt and you need /project/file.txt
I would split according to os default separator (so it would work with windows paths as well), then reformat only the 2 last parts with the proper format (sep+path) and join that.
import os
#os.sep = "/" # if you want to test that on Windows
s = "/home/user/project/file.txt"
path_end = "".join(["{}{}".format(os.sep,x) for x in s.split(os.sep)[-2:]])
result:
/project/file.txt
I assume the following code should works:
def upload_file(project_id, filename, dropbox_token):
dbx = dropbox.Dropbox(dropbox_token)
abs_path = os.path.abspath(filename)
directory, file = os.path.split(abs_path)
_, directory = os.path.split(directory)
dropbox_path = os.path.join(directory, file)
with open(abs_path, "rb") as f:
dbx.files_upload(f.read(), dropbox_path, mute=True)
link = dbx.files_get_temporary_link(path=dropbox_path).link
return link