I want to open and read all csv file in a specific folder.
I'm on OS X El Capitan version 10.11.6, and I'm using Python 2.7.10.
I have the following function in phyton file:
def open_csv_files(dir):
for root,dirs,files in os.walk(dir):
for file in files:
if file.endswith(".csv"):
f= open(file)
print "FILE OPEN, AND DO SOMETHING... "
f.close
return
I call open_csv_file(./dati/esempi)
This procedure return
IOError: [Errno 2] No such file or directory: 'sensorfile_1.csv'
I try to call the procedure with absolute path /Users/Claudia/Desktop/Thesis/dati/esempi/ but I have the same error.
Moreover I define another procedure that print all filename in folder, this procedure print correctly all filenames in folder.
Thanks for the help.
You need to build absolute path to file based on values of root (base dir) and file name.
import os
def open_csv_files(directory):
for root, dirs, files in os.walk(directory):
for file_name in files:
if file_name.endswith(".csv"):
full_file_path = os.path.join(root, file_name)
with open(full_file_path) as fh:
print "Do something with", full_file_path
Related
I try to follow an example in an online course to create a function to change the files name by omitting the numbers from the files name. But I got this error:
os.rename(file_name, file_name.translate(None,"0123456789"))
WindowsError: [Error 2] The system cannot find the file specified
But the code runs fine on the video that I watched
import os
def rename_files():
# (1) get file naems from a folder
file_list = os.listdir(r"C:\prank")
#print (file_list)
saved_path = os.getcwd()
print (" Currnet Wroking Directory is " +saved_path)
os.chdir(r"C:\prank")
# (2) for each file, rename filename
for file_name in file_list:
os.rename(file_name, file_name.translate(None,"0123456789"))
os.chdir (saved_path)
rename_files()
Try this one out, there is no need to change the working directory, its not a good practice. Also if you are renaming files in C: you may have to execute the code as administrator.
import os
def rename_files():
# directoy with the files
directory=r"C:\prank"
# (1) get file names from a folder
file_list = os.listdir(directory) # this is the folder with the files
# (2) for each file, rename filename
for file_name in file_list:
new_name=''.join([s for s in file_name if s not in ['0','1','2','3','4','5','6','7','8','9']]) # remove numbers
os.rename(os.path.join(directory,file_name),os.path.join(directory,new_name))
rename_files()
I have my python code that runs a C++ code, which takes files in another folder as input.
I have my codes in folder A, and the input files are in folder B, and I have been trying this:
path = 'C:/pathToInputFiles'
dirs = os.listdir(path)
for path in dirs:
proc = subprocess.Popen([fullPathtoCppCode, inputFiles])
However, I keep receiving WindowsError: [Error 2] The system cannot find the file specified
The only way it works is when I put the C++ executable file in the same folder of the input files, which I am avoiding to do.
How can I make python reads the file path properly?
Try using os.path.join after your for statement.
path = os.path.join(directory, filename)
for example
def test(directory):
for filename in os.listdir(directory):
filename = os.path.join(directory, filename)
proc = subprocess.Popen([fullPathtoCppcode, inputFiles])
I want to open a .txt file using python in odoo and read its contents. Where should I place this .txt file for it to be opened.
What I tried,
I kept the file in the same directory as models.py and in models.py wrote the code to open the file,
try:
logFile = open('log.txt', 'r')
but this file is not opened. Please help, where should I keep log.txt for it to be located, or can I set any path for models.py to understand where log.txt is?
I just found the solution, I set the absolute path to where the .txt file is found.
import os
path = os.path.expanduser('<absolute path to .txt file>')
logFile = open(path, 'r')
this worked.
This is Python Code may be help you:
with open("log.txt") as file: # Use file to refer to the file object
data = file.read()
do something with data
print data
File handling in Python See this link : http://pymbook.readthedocs.org/en/latest/file.html
I am trying to set up a program to put my minecraft server world into a zip and place it into another directory on another drive (/media/500gb/MinecraftWorldBackups)
But I keep getting this error
Although the folder doesn't contain a folder or file called 'h'
What do I need to do to fix this I believe it is due to file and folder?
#!/usr/bin/env python
import time, zipfile
while True:
FileName = 'MinecraftBackup_' + str(int(time.time()))
Path = '/home/bertie/Desktop/FeedTheBeastServer/world/'
print(FileName)
Zip = zipfile.ZipFile('/media/500gb/MinecraftWorldBackups/'+FileName+'.zip','w')
for each in Path:
print(each)
try: Zip.write(Path + each)
except IOError: None
Zip.Close()
print('Done')
time.sleep(60)
When I try to run module to open file in python with the following code:
from sys import argv
script,filename = argv
txt = open(filename)
print "Here's your file %r:" % (filename)
print txt.read()
print "Type the filename again:"
file_again = raw_input(">")
txt_again =open(file_again)
print txt_again.read()
Gives me the following error:
IOError: [Errno 2] No such file or directory: 'ex15_sample.txt'
Probably the file ex15_sample.txt is not in the current directory of execution ? Did you try to write the full path of the file at the raw_input step, ie /tmp/ex15_sample.txt ?