i'm trying to run python script which already ran on test environment.
already checked if the path correct and if the file in it.
checked in shell that the file exist.
current code is :
# Open a file
path = 'C:\\Users\\tzahi.k\\Desktop\\netzer\\'
dirs = os.listdir( path )
fileslst = []
alertsCode = (some data)
# loop over to search the relative file
for file in dirs:
if "ALERTS" in file.upper() :
fileslst.append(file)
fileslst.sort()
#open and modify the latest file
with open(fileslst[-1], 'rb') as csvfile:
csvReader = csv.reader(csvfile)
clean_rows = [row for row in csvReader if not any(alert in row[2] for alert in alertsCode)]
error :
IOError:error 2 no such file or directory:'file name'
when i debug in shell i see the path and files
what am i doing wrong?
os.listdir() lists the files relative to the directory.
You need to add the full directory path to the filename for it to be an absolute path again:
with open(os.path.join(path, fileslst[-1]), 'rb') as csvfile:
Related
I have this which makes directories and then creates txt files. But it creates the txt files in the main parent dir. I want one info.txt in each newly created folder. How can I fix?
import os
parent_dir = r"C:\America"
text_file = r"C:\dir.txt"
info_file = "info.txt"
with open(text_file, 'r', encoding='utf-8') as f:
lines = f.read().splitlines()
for line in lines:
path = os.path.join(parent_dir, line)
new_dir = os.mkdir(path)
with open(path + info_file, 'w') as filehandle:
filehandle.writelines('test')
You'll want to use os.path.join() when creating that final filename too, instead of path + info_file.
If path doesn't have a slash at the end, with e.g. path = "test" you'd get "testinfo.txt", which is a relative pathname for a file in the working directory – os.path.join() adds the delimiter if it isn't there. (To be clear, in your case, if line is "test", path would end up being "C:\America\test", and adding info.txt to that, you'd get "C:\America\testinfo.txt".)
You may also wish to use os.makedirs() to create the full folder path, if necessary; os.mkdir() would raise an exception if an intermediate folder isn't there, or if the target folder already is there.
with open(text_file, 'r', encoding='utf-8') as f:
lines = f.read().splitlines()
for line in lines:
path = os.path.join(parent_dir, line)
os.mkdir(path)
info = os.path.join(path, info_file)
with open(info, 'w') as filehandle:
filehandle.writelines('test')
i am trying to write several .csv file into one specific directory
here is my code
with open(f+'.csv', 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(["index", "B", "G", "R"])
for row in rows:
writer.writerow(row)
writer.writerow(["Mean", mean_b/total_b, mean_g/total_g, mean_r/total_r])
writer.writerow("STD", np.sqrt(var_b/total_b), np.sqrt(var_g/total_g), np.sqrt(var_r/total_r))
i have created the csv file into the directory which is same as the .py file
however i would like to create a directory and create my csv file in it
i know i need to us os.makedirs() function
but i don't know whether i have to create the directory first and designate the path for the csv file or i simply put the directory name into the open() function
please help me
Instead of using os I recommend using the pathlib module.
You can create a directory with:
path = Path('path/to/dir')
path.mkdir(parents=True)
to create the directory and all its missing parent dirs.
After doing this you can create a file in the new directory with
fpath = (path / 'filename').with_suffix('.csv')
with fpath.open(mode='w+') as csvfile:
# your csv writer code
I would simply create the directory and except directory exists error
try:
os.mkdir("./CSV")
except OSError as e:
print("Directory exists")
with open("./CSV/" + f + ".csv", newline="") as csvfile:
[...]
You can add a check for the directory like this just before open statement
dir_path = 'folder_to_save_csv_file_in'
if not os.path.isdir(dir_path):
os.makedirs(dir_path)
with open('{file_path}.csv'.format(file_path=os.path.join(dir_path, file_name), 'w+') as csv_file:
....
I am trying to move text files from one folder to another by reading a path from a csv file. First I create the target folder in which I want to move my files from the existing folder. I read the existing folder path from csv file. I am working on a Windows platform.
This is my code :
import os
import csv
import shutil
#csv_filename = raw_input('Enter CSV filename:')
with open('insurance_sample.csv') as csvfile:
readCSV = csv.reader(csvfile, delimiter = ';')
header = next(readCSV)
count = 0
for row in readCSV:
dirname = "/".join(('Sorted_Program',row[1],row[4],row[3],row[7]))
#if not os.path.exists(dirname):
#os.makedirs(dirname)
path = row[10]
moveto = dirname
print path
print moveto
print os.path.isfile(path)
files = os.listdir(path)
print files
files.sort()
for f in files:
src = path + f
dst = moveto + f
break
I am getting this error after running the code:
C:\Users\Ashwin\Desktop\p\newDir\Archives\Beta\A380_1
Sorted_Program/A380/AFR/69/Flight_Test
False
Traceback (most recent call last):
File "C:\Users\Ashwin\Desktop\p\newDir\sar.py", line 19, in <module>
files = os.listdir(path)
WindowsError: [Error 3] The system cannot find the path specified: 'C:\\Users\\Ashwin\\Desktop\\p\\newDir\\Archives\\Beta\\A380_1/*.*
Please let me know if the question is still confusing and I will try to explain in more detail.
So it seems there are two issues here:
You are creating a directory reference with / whereas windows directories require \
Your code does not prefix the new directory structure with \
Try the following modification:
dirname = "\\" + "\\".join(('Sorted_Program',row[1],row[4],row[3],row[7]))
So I am able to parse through multiple csv/txt files, remove the columns I wanted and saved them in a new file. I now want this to go to a new folder so that they are separate from the original files. I have 40 total original files and when I run my script, it moves 32 of the files over to the new folder, but I get a Permission denied when it starts on file 33. Why would this be happening if it works on so many of the previous files?
import os, sys, csv
path = ('C://Users//nelsonj//Desktop//Master_Project')
trimmed_files = ('C://Users//nelsonj//Desktop//Master_Project//Trimmed_Files')
for filename in os.listdir(path):
pref_cols = [0,1,2,4,6,8,10,12,14,18,20,22,24,26,30,34,36,40]
with open(filename, "rb") as sitefile:
with open(os.path.join(trimmed_files, filename.rsplit('.',1)[0] + "_trim.txt"), 'w') as output_file:
reader = csv.reader(sitefile, delimiter=',')
writer = csv.writer(output_file)
for row in reader:
new_row = list(row[i] for i in pref_cols)
writer.writerow(new_row)
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")