use a function on every item in a list python - python

Hello I am trying to build a tool that will compress a list of folders and rename the compressed file, this list of the names of folders I want to compress are located in a .txt file, the .txt is something like this:
james, 5005
kyle, 02939
Betty, 40234
I have used multiple methods to try and build this code but I keep getting a python error set object is not subscriptable and I have no idea what to do to rectify this and on how to continue from here. Can I not use shutil.make_archive with dictionaries or can I use lists? because I would like to run this function down the first column and to rename the files i am creating using the second column. I am using python 3, and any help would be great!
import os
import shutil
x = input("Input Path your user list: ")
filename = input("Input user file name: ")
changedir = input("Input where your folders are: ")
os.chdir(changedir)
userfile = x + filename + ".txt"
print("Awesome your path is now", userfile)
with open(userfile, "rt") as userfileone:
count = 0
while 1:
buffer = userfileone.read(8192*1024)
if not buffer: break
count += buffer.count('\n')
print("It is indicated that there are", count + 1, "in the file")
with open(userfile, "rt") as f:
lines = f.readlines()
dic = {}
for x in lines:
x = x.strip().split(',')
dic[x[0]]=tuple(x[1:])
for i in dic:
shutil.make_archive(i, "zip", dic[i])

It seems like you are looking for the map function.

Related

Reading files and printing only the filename in python

I am new to python, I need to develop a simple code in which I have to take a directory as a user input and then read all the txt.files in there (containing numbers) then based on the numbers I have to generated an output based on the txt files names.
For example, I have two files one name de and the other is named co, each file contains a number say 1 for co and 2 for de, I need to read the program to read the number from the file, then arrange the output based on the files name and the numbers arranged, which means the output here should be code, as co contains 1 and de contain 2..
This is the code so far as I am getting users directory as input
import glob
import os
dirname = input("Please input directory path ")
path = os.path.join(dirname,"**")
for x in glob.glob(path, recursive=True):
print(x)
You simply have to read from all the matching .txt files from your glob results. Put everything in a dict and sort it based on the value.
import glob
import os
dirname = input("Please input directory path ")
path = os.path.join(dirname, "**", "*.txt")
fileValues = {}
for x in glob.glob(path, recursive=True):
with open(x, 'r') as f:
# Use filename as key and assign its value to it
fileValues[os.path.basename(x)] = int(f.read())
# Sort the dictionary based on the values and extract the keys in order
sortedFiles = dict(sorted(fileValues.items(), key=lambda x: x[1])).keys()
# sortedFiles is now a list containing all the filenames in ascending order of their values
# Print it, or use it however you want
print(sortedFiles)
For your example, sortedFiles is now ["co.txt", "de.txt"]
You can get "code" from this by replacing all the .txt and joining the list with ''
''.join(x.replace('.txt', '') for x in sortedFiles)

How do I count unique names?

I am trying to count up unique names that start with "From:" from a file name. However, I keep getting a long list of numbers. What is my code actually reading and how do I fix this?
count = 0
name = []
fname = input("What is your file name? Enter it here: ")
try:
fname = open(fname)
name = set(f.readlines())
except:
print ("That file does not exist.")
for name in fname:
if name.startswith("From:"):
count = len(name)
print (count)
We can make use of set to hold all required names and find its length to get the count:
file_name = input("What is your file name? Enter it here: ")
s = set()
with open(file_name) as f:
for name in f:
if name.startswith('From:'):
s.add(name)
print(len(s))
Try this:
words = []
count = 0
with open ("unique.txt","r") as f:
# Get a list of lines in the file and covert it into a set
words = set(f.readlines())
FromWords=[]
for word in words:
if word.startswith("From:"):
FromWords.append(word)
print(len(FromWords))
First, we filter out all duplicate words and then look for the words which start with From: and this may aid in faster processing if you're dealing with the big amount of data.
let me know if you need any help in this regard.

How do I organize data in alphabetical order?

So I have some pieces of data stored in a folder as .txt, e.g. FRED.txt & BOB.txt, which in the text file contain their 5 random numbers chosen from 1 to 10 and I am stuck as to how I can print their names (in alphabetical order) along with their highest random number. I know that I have to use the glob or os libraries but I don't really know where to go with them.
So far I have this...
import glob, os
dataFile = open("directory_pathway", "r+")
dataFile.read()
# Somehow printing names & highest number here.
dataFile.close()
Any help is much appreciated. Thanks :)
Get only text file from the input directory by glob module.
Use for loop to iterate every text file.
Read file content.
Get max number from the file content.
Add into result dictionary.
Sort dictionary keys and print values.
input: Following contents in FRED.txt file
2
4
6
8
10
code:
import glob
import os
dir_path = "/home/vivek/Desktop/stackoverflow/input"
text_files = glob.glob(dir_path+"/*.txt")
print "Test Files:", text_files
result = {}
for i in text_files:
# Read file content.
with open(i, 'rb') as fp:
data = fp.read()
max_no = max([int(j) for j in data.split()])
result[os.path.basename(i)] = max_no
#- Sort and print File names.
sorted_file_names = sorted(result.keys())
for i in sorted_file_names:
print "File Name: %s, MAx Random Number: %d"%(i, result[i])
output:
Test Files: ['/home/vivek/Desktop/stackoverflow/input/AOB.txt', '/home/vivek/Desktop/stackoverflow/input/ABO.txt', '/home/vivek/Desktop/stackoverflow/input/FRED.txt', '/home/vivek/Desktop/stackoverflow/input/BOB.txt']
File Name: ABO.txt, MAx Random Number: 9
File Name: AOB.txt, MAx Random Number: 9
File Name: BOB.txt, MAx Random Number: 9
File Name: FRED.txt, MAx Random Number: 10
vivek#vivek:~/Desktop/stackoverflow/input$
import glob, os, re
names = []
path = os.path.join('path_to_dir', '*.txt')
for filename in glob.glob(path):
names.append(filename)
names.sort()
for filename in names:
print(re.search(r'\w+.txt', filename).group(0))
text = open(filename, 'r')
data = text.read().split()
print(max(data, key = int), '\n')
text.close()
raw_input()
import os
result_dict = {}
for i in sorted([i for i in os.listdir("/path/to/folder/") if i.endswith(".txt")]):
f = open(i)
a = f.readlines()
num = sorted([int(j.strip()) for j in a])
print num
result_dict[i] = num[-1]
for i,j in sorted(result_dict.items(), key=lambda s: s[0]):
print i,j
sort the file names found with glob, map the contents to int and print the filename f and the max:
import glob
import os
path = "path/"
for f in sorted(glob.glob(os.path.join(path,"*.txt"))):
with open(os.path.join(path, f)) as fl:
print("Filename: {}\nMax value: {}".format(f, max(map(int, fl))))
map returns a map object so we don't need to create a list to find the max, we only store one line/value at a time.
sorted(glob.glob("*.txt")) will get you the list of filenames, sorted. Then iterate over that list, open each file, and print whatever you like.

How to save the output as a .txt file?

I want to save the output as a text file on my system. The name of the output file should get from the user in command prompt.
output = input("Enter a name for output file:")
my_file = open('/output.txt', "w")
for i in range(1, 10):
my_file.write(i)
Is this correct way of doing??
Do like this
output = raw_input("Enter a name for output file:")
my_file = open(output + '.txt', "w")
for i in range(1, 10):
my_file.write(str(i))
You can do the following:
import os
# you can use input() if it's python 3
output = raw_input("Enter a name for output file:")
with open("{}\{}.txt".format(os.path.dirname(os.path.abspath(__file__)), output), "w") as my_file:
for i in range(1, 10):
my_file.write("".format(i))
At this example we are using the local path by using os.path.dirname(os.path.abspath(__file__)) we will get the current path and we will add it output.txt
To read more about abspath() look here
To read more about with look here
write method in you case will raise a TypeError since i needs to be a string
So couple of changes I made. You need to do something like:
output + '.txt'
to use the variable output as the file name.
Apart from that, you need to convert the integer i to a string by calling the str() function:
str(i)
becuase the write function only takes strings as input.
Here is the code all together:
output = raw_input("Enter a name for output file: ")
my_file = open(output + '.txt', 'wb')
for i in range(1, 10):
my_file.write(str(i))
my_file.close()
Hope this helps!
You can do it in one line so you will have your txt file in your .py file path:
my_file=open('{}.txt'.format(input('enter your name:')),'w')
for i in range(1, 10):
my_file.write(str(i))
my_file.close()
Note: if you are using python 2.x use raw_input() instead of input .

In Python: Search for two words in multiple texts using raw_input

I want to open (raw_input) multiple text files in a directory, give them a name (Document 1, Document 2...), search (raw_input) for two words using "OR" and then put the search word (without characters ".,/"" only the words in lower case) and names of the files containing the word/words in a list or new text file:
I've tried to put the files into a dictionary, but I don't really know if that is a stupid idea?
I don't know how to let the user search for one or two words (via raw_input) in all the files at the same time. Can you help me or give me a hint?
I want it to print something like:
SearchWord "found in " Document 1, python.txt
SearchWord "found in " Document 3, foobar.txt
import re, os
path = raw_input("insert path to directory :")
ex_library = os.listdir(path)
search_words = open("sword.txt", "w") # File or maybe list to put in the results
thelist = []
for texts in ex_library:
file = os.path.join(path, texts)
text = open(file, "r")
textname = os.path.basename(texts)
print textname
for names in textname.split():
thelist.append(names)
text.close()
print thelist
print "These texts are now open"
print "###########################"
count = 0
for y in ex_library:
count = count + 1
print count
print "texts total"
d ={}
for x in range(count):
d["Document {0}".format(x)] = None # I would like the values to be the names of the texts (text1.txt)

Categories

Resources