I am (trying) to write a tool that will open a file based on user input.
I want to eventually write the results of the script into a file and store it into the same directory as the input file.
I currently have this
from Bio import SeqIO
import os, glob
var = raw_input("what is the path to the file (you can drag and drop):")
fname=var.rsplit("/")[-1]
fpath = "/".join(var.rsplit("/")[:-1])
os.chdir(fpath)
#print os.getcwd()
#print fname
#for files in glob.glob("*"):
# print files
with open(fname, "rU") as f:
for line in f:
print line
I do not understand why I cannot open the file. Both the "os.getcwd" and the "glob.glob" part show that I successfully moved to the users directory. In addition, the file is in the correct folder. However, I cannot open the file...
any suggestions would be appreciated
Try this to open the file and get the path to the file:
import os
def file_data_and_path(filename):
if os.path.isfile(filename):
path = os.path.dirname(filename)
with open(filename,"rU") as f:
lines = f.readlines()
return lines,path
else:
print "Invalid File Path, File Doesn't exist"
return None,None
msg = 'Absolute Path to file: '
f_name = raw_input(msg).strip()
lines,path = file_data_and_path(f_name)
if lines != None and path != None:
for line in lines:
print lines
print 'Path:',path
mmm asume you want validations, this maybe can help you :)
def open_files(**kwargs):
arc = kwargs.get('file')
if os.path.isfile(arc):
arc_f = open(arc, 'r')
lines = arc_f.readlines()
for line in lines:
print line.strip()
if __name__ == "__main__":
p = raw_input("what is the path to the file (you can drag and drop):")
open_files(file=p)
Related
I'm trying to copy files from directory A, to directory B, based on a txt file containing the list of files to be extracted - located in directory B. I referred to this code: How to extract files from a particular folder with filename stored in a python list?
but it doesn't seem to enter the if (where I have put the 'in here' printout). Could someone tell me what I am doing wrong?
This is the code:
import os
import shutil
def read_input_file():
my_file = open("/mnt/d/Downloads/TSU/remaining_files_noUSD_19Jan.txt", "r")
# reading the file
data = my_file.read()
data_into_list = data.split("\n")
#print(data_into_list)
my_file.close()
return data_into_list
def filter_data(list_of_files):
path="/mnt/e/Toyota Smarthome/Untrimmed/Videos_mp4"
path_to_be_moved="/mnt/d/Downloads/TSU"
#print(list_of_files)
for file in os.listdir(path):
#print(file)
if file in list_of_files:
print("in here")
print(file)
shutil.copytree(path,path_to_be_moved)
#os.system("mv "+path+file+" "+path_to_be_moved)
if __name__ == "__main__":
list = read_input_file()
filter_data(list)
I am using python3 via WSL.
the mp4 folder contains multiple videos, and the output of "
read input file
is as follows
"
Thank you!
I think copytree from shutil has another purpose to just move file, it moves an entire structure. I'd use shutil.move
import os
import shutil
def read_input_file():
my_file = open("list.txt", "r")
data = my_file.read()
data_into_list = data.split("\n")
my_file.close()
return data_into_list
def filter_data(list_of_files):
path="directoryA/"
path_to_be_moved="directoryB/"
for file in os.listdir(path):
# print(file)
if file in list_of_files:
print("in here")
print(file)
shutil.move(path+file,path_to_be_moved+file)
mylist = read_input_file()
filter_data(mylist)
just saw your update, be careful, data_into_list = data.split("\n") is for a file.txt with a list separated with an enter. yours is with a comma and space so you'll have to change that.
Also you shouldn't use list as a variable name, mylist for example is better. list() is used to create list
I want to write a program for this: In a folder I have n number of files; first read one file and perform some operation then store result in a separate file. Then read 2nd file, perform operation again and save result in new 2nd file. Do the same procedure for n number of files. The program reads all files one by one and stores results of each file separately. Please give examples how I can do it.
I think what you miss is how to retrieve all the files in that directory.
To do so, use the glob module.
Here is an example which will duplicate all the files with extension *.txt to files with extension *.out
import glob
list_of_files = glob.glob('./*.txt') # create the list of file
for file_name in list_of_files:
FI = open(file_name, 'r')
FO = open(file_name.replace('txt', 'out'), 'w')
for line in FI:
FO.write(line)
FI.close()
FO.close()
import sys
# argv is your commandline arguments, argv[0] is your program name, so skip it
for n in sys.argv[1:]:
print(n) #print out the filename we are currently processing
input = open(n, "r")
output = open(n + ".out", "w")
# do some processing
input.close()
output.close()
Then call it like:
./foo.py bar.txt baz.txt
You may find the fileinput module useful. It is designed for exactly this problem.
I've just learned of the os.walk() command recently, and it may help you here.
It allows you to walk down a directory tree structure.
import os
OUTPUT_DIR = 'C:\\RESULTS'
for path, dirs, files in os.walk('.'):
for file in files:
read_f = open(os.join(path,file),'r')
write_f = open(os.path.join(OUTPUT_DIR,file))
# Do stuff
Combined answer incorporating directory or specific list of filenames arguments:
import sys
import os.path
import glob
def processFile(filename):
fileHandle = open(filename, "r")
for line in fileHandle:
# do some processing
pass
fileHandle.close()
def outputResults(filename):
output_filemask = "out"
fileHandle = open("%s.%s" % (filename, output_filemask), "w")
# do some processing
fileHandle.write('processed\n')
fileHandle.close()
def processFiles(args):
input_filemask = "log"
directory = args[1]
if os.path.isdir(directory):
print "processing a directory"
list_of_files = glob.glob('%s/*.%s' % (directory, input_filemask))
else:
print "processing a list of files"
list_of_files = sys.argv[1:]
for file_name in list_of_files:
print file_name
processFile(file_name)
outputResults(file_name)
if __name__ == '__main__':
if (len(sys.argv) > 1):
processFiles(sys.argv)
else:
print 'usage message'
from pylab import *
import csv
import os
import glob
import re
x=[]
y=[]
f=open("one.txt",'w')
for infile in glob.glob(('*.csv')):
# print "" +infile
csv23=csv2rec(""+infile,'rb',delimiter=',')
for line in csv23:
x.append(line[1])
# print len(x)
for i in range(3000,8000):
y.append(x[i])
print ""+infile,"\t",mean(y)
print >>f,""+infile,"\t\t",mean(y)
del y[:len(y)]
del x[:len(x)]
I know I saw this double with open() somewhere but couldn't remember where. So I built a small example in case someone needs.
""" A module to clean code(js, py, json or whatever) files saved as .txt files to
be used in HTML code blocks. """
from os import listdir
from os.path import abspath, dirname, splitext
from re import sub, MULTILINE
def cleanForHTML():
""" This function will search a directory text files to be edited. """
## define some regex for our search and replace. We are looking for <, > and &
## To replaced with &ls;, > and &. We might want to replace proper whitespace
## chars to as well? (r'\t', ' ') and (f'\n', '<br>')
search_ = ((r'(<)', '<'), (r'(>)', '>'), (r'(&)', '&'))
## Read and loop our file location. Our location is the same one that our python file is in.
for loc in listdir(abspath(dirname(__file__))):
## Here we split our filename into it's parts ('fileName', '.txt')
name = splitext(loc)
if name[1] == '.txt':
## we found our .txt file so we can start file operations.
with open(loc, 'r') as file_1, open(f'{name[0]}(fixed){name[1]}', 'w') as file_2:
## read our first file
retFile = file_1.read()
## find and replace some text.
for find_ in search_:
retFile = sub(find_[0], find_[1], retFile, 0, MULTILINE)
## finally we can write to our newly created text file.
file_2.write(retFile)
This thing also works for reading multiple files, my file name is fedaralist_1.txt and federalist_2.txt and like this, I have 84 files till fedaralist_84.txt
And I'm reading the files as f.
for file in filename:
with open(f'federalist_{file}.txt','r') as f:
f.read()
I need to open a file from a different directory without using it's path while staying in the current directory.
When I execute the below code:
for file in os.listdir(sub_dir):
f = open(file, "r")
lines = f.readlines()
for line in lines:
line.replace("dst=", ", ")
line.replace("proto=", ", ")
line.replace("dpt=", ", ")
I get the error message FileNotFoundError: [Errno 2] No such file or directory: because it's in a sub directory.
Question: Is there an os command I can use that will locate and open the file in sub_dir?
Thanks! -let me know if this is a repeat, I searched and couldn't find one but may have missed it.
os.listdir() lists only the filename without a path. Prepend these with sub_dir again:
for filename in os.listdir(sub_dir):
f = open(os.path.join(sub_dir, filename), "r")
If all you are doing is loop over the lines from the file, just loop over the file itself; using with makes sure that the file is closed for you when done too. Last but not least, str.replace() returns the new string value, not change the value itself, so you need to store that return value:
for filename in os.listdir(sub_dir):
with open(os.path.join(sub_dir, filename), "r") as f:
for line in f:
line = line.replace("dst=", ", ")
line = line.replace("proto=", ", ")
line = line.replace("dpt=", ", ")
You must give the full path if those files are not in the current directory:
f = open( os.path.join(sub_dir, file) )
I would not use file as a variable name, maybe filename, since this is used to create a file object in Python.
Code to copy files using shutil
import shutil
import os
source_dir = "D:\\StackOverFlow\\datasets"
dest_dir = "D:\\StackOverFlow\\test_datasets"
files = os.listdir("D:\\StackOverFlow\\datasets")
if not os.path.exists(dest_dir):
os.makedirs(dest_dir)
for filename in files:
if file.endswith(".txt"):
shutil.copy(os.path.join(source_dir, filename), dest_dir)
print os.listdir(dest_dir)
i have this python script that open a file dialog and select a text file than copy its content to another file.
when i open the second file it still empty
can anyone help me to solve this problem ?
OPenDirectory.py
#!/usr/bin/python
import Tkinter
import tkFileDialog
''''Open txt files in the selected path '''
def OpenRead():
Tkinter.Tk().withdraw()
in_path = tkFileDialog.askopenfile(initialdir = 'C:\Users\LT GM\Downloads', filetypes=[('text files', ' TXT ')])
readingFile = in_path.read()
writeFile = open ('copiedFile.txt', 'w')
writeFile.write(readingFile)
print " we'r done!!"
in_path.close()
writeFile.close()
if __name__== "__main__":
OpenRead()
You can use shutil.copyfile, there is no need to open or read the file.
from shutil import copyfile
copyfile("source","dest")
So for your code:
def OpenRead():
Tkinter.Tk().withdraw()
in_path = tkFileDialog.askopenfile(initialdir = 'C:\Users\LT GM\Downloads', filetypes=[('text files', ' TXT ')])
copyfile(in_path.name, 'copiedFile.txt')
print " we'r done!!"
if __name__== "__main__":
OpenRead()
The file is also going to be copied to you pwd so if you want it save somewhere in particular you need to pass the full path.
Line by line way of copying from file to file:
#!/usr/bin/python
from os import listdir
from os.path import isfile, join
def readWrite():
mypath = 'D:\\'
files = [f for f in listdir(mypath) if isfile(join(mypath, f))]
for file in files:
if file.split('.')[1] == 'txt':
outputFileName = 'out-' + file
with open(mypath+outputFileName, 'w') as w:
with open(mypath+file) as f:
for l in f:
print l
w.write(l)
if __name__== "__main__":
readWrite()
UPDATE:
updated the code above, so it reads all the txt files in a specified directory and copies them into other files. You can play with directories how you like. I also added a "print l" which will print the contents of the incoming file.
readingFile = in_path.read()
reads the contents of the file and puts it in variable readingFile
suppose the contents of the file is,say, hello the value of readingFile will be hello
destinationFile = readingFile, '.txt'
destinationFile is a tuple with values '<'contents of file'>','.txt'
instead use destinationFile = readingFile+'.txt'
this will work. but the end result would not be what you are expecting.the result would be a file with name as contents of the reading file with contents also the same. better specify a file name in destinationFile like destinationFile = 'destfile.txt'
Why don't you just use os, and the shell copy command.
import os
start= '~/user/file'
dest= '~/user/folder1/file'
os.system('cp %s %s' %(start,dest))
I need to open a file from a different directory without using it's path while staying in the current directory.
When I execute the below code:
for file in os.listdir(sub_dir):
f = open(file, "r")
lines = f.readlines()
for line in lines:
line.replace("dst=", ", ")
line.replace("proto=", ", ")
line.replace("dpt=", ", ")
I get the error message FileNotFoundError: [Errno 2] No such file or directory: because it's in a sub directory.
Question: Is there an os command I can use that will locate and open the file in sub_dir?
Thanks! -let me know if this is a repeat, I searched and couldn't find one but may have missed it.
os.listdir() lists only the filename without a path. Prepend these with sub_dir again:
for filename in os.listdir(sub_dir):
f = open(os.path.join(sub_dir, filename), "r")
If all you are doing is loop over the lines from the file, just loop over the file itself; using with makes sure that the file is closed for you when done too. Last but not least, str.replace() returns the new string value, not change the value itself, so you need to store that return value:
for filename in os.listdir(sub_dir):
with open(os.path.join(sub_dir, filename), "r") as f:
for line in f:
line = line.replace("dst=", ", ")
line = line.replace("proto=", ", ")
line = line.replace("dpt=", ", ")
You must give the full path if those files are not in the current directory:
f = open( os.path.join(sub_dir, file) )
I would not use file as a variable name, maybe filename, since this is used to create a file object in Python.
Code to copy files using shutil
import shutil
import os
source_dir = "D:\\StackOverFlow\\datasets"
dest_dir = "D:\\StackOverFlow\\test_datasets"
files = os.listdir("D:\\StackOverFlow\\datasets")
if not os.path.exists(dest_dir):
os.makedirs(dest_dir)
for filename in files:
if file.endswith(".txt"):
shutil.copy(os.path.join(source_dir, filename), dest_dir)
print os.listdir(dest_dir)