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 am using Python 3.7.
I want to check the file sizes of a directory with the file path to a text file.
This is my code:
import os
import glob
# folder path
folderpath = 'C:/Test'
# Get a list of files in my folder
list_of_files = filter( os.path.isfile,
glob.glob(folderpath + '*') )
# get list of files with the size in my folder
files_with_size = [ (file_path, os.stat(file_path).st_size)
for file_path in list_of_files ]
# Iterate over the files and write them to a file
for file_path, file_size in files_with_size:
with open('c:/Test/filesize.txt', 'w') as f:
print(file_size, ' -->', file_path)
I can print the result in the Python console, but I cannot manage to write my result to a text file.
Can anybody help me?
Regards,
Jan
I would ameloriate your code following way
for file_path, file_size in files_with_size:
with open('c:/Test/filesize.txt', 'a') as f:
print(file_size, ' -->', file_path, file=f)
changes: use a (append) mode and use f as value for named argument file of print function.
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 am working on a script in which I have a text file that contains name like
abc_3
bcd_5
def_3
And I have a folders like:
abc
bcd
def
The script should rename the folder to the filename with underscore if the name is similar.So that my folders will also become like
abc_3
bcd_5
def_3.
So far I Have written this:
import os, sys, subprocess
import csv
import re
import glob
Folder_dir = os.listdir('/path/to/folders')
for folders in Folder_dir:
print folders
Txt_file = open("/vedata/detectionname.txt", "r") # The text file location
for line in Txt_file:
if folders in line: #How to match folders name??
print line
The following should work for you:
import os
folders_path = '/path/to/folders'
folders = os.listdir(folders_path)
for folder in folders:
with open("folders.txt", "r") as f:
for line in f.read().splitlines():
if folder in line:
os.rename(
os.path.join(folders_path, folder),
os.path.join(folders_path, line)
)
Note:
with statement allows you to make sure that the file would be closed even is an exception is raised.
Changing folders name is as simple as os.rename(old, new):
path = '/path/to/folders'
folder_dir = os.listdir(folders_path)
for folder_name in folder_dir:
with open("/vedata/detectionname.txt", "r") as txt_file:
for line in txt_file.readlines():
line_stripped = line.split('-')[0]
if folder_name.startswith(line_stripped):
os.rename(path + folder_name, path + line)
break
I've also changed the code to with to open as it closes it automatically once done.
I referred the path so the rename explicitly works on full path.
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)