Python getting content out of files in located in different folders - python-3.x

Hy together, I have Problem with my code, because it dont do what it should. I will describe what I actualy want to do. I have Folder caled test with is the root folder with a few web page folders containing php files from wich I want to get some content and write it in a txt file. The codes run and dont give any errors but it also don't create the words.txt file with the content I wanted. Any Idea why?
from __future__ import print_function
import io
import os
import re
rootdir ='.../test' # I write here the full path but due to privacy reassons only the folders name
for subdir, dirs, files in os.walk(rootdir):
for file in files:
if file.endswith(".php"):
with io.open(file, encoding="utf-8") as f, io.open('words.txt', 'w',encoding="utf-8") as g:
for line in f:
h = re.sub(r"$slimname = '([^']+)'", r"\1", line.rstrip())
m = re.sub(r"'alwaysfound_text' => '([^']+)'", r"\1", line.rstrip())
l = re.sub(r"'alwaysfound_place' => '([^']+)'", r"\1", line.rstrip())
j = re.sub(r"'alwaysfound_job' => '([^']+)'", r"\1", line.rstrip())
k = re.sub(r"var_keyword_hidden_generic' => '([^']+)'", r"\1", line.rstrip())
print (h, m, l, j, k, file = g)

a few issues with the code:
you open file for 'w' but probably want 'a' (append)
indentation is minor mess, but shouldn't be a problem
you open filename but disregard its subdirectory - use with io.open(os.path.join(subdir, file), encoding="utf-8") as f

You probably rewrite the file word.txt for every next "file in files" as you open it with the mode "w" (which means "rewrite"). Try use mode "a" (which means "append").

I found a blank between print statement and the first brace.
This should result in a syntax error.
Remove it and test your code again.

Related

Finding a file by extension

I am trying to find files with .desktop extension in a specific directory in Python3. I tried the code snippet below but it didn't work as I wanted. I want it to be a single string value.
import os, fnmatch
desktopfile = configparser.ConfigParser ()
def find(pattern, path):
result = []
for root, dirs, files in os.walk(path):
for name in files:
if fnmatch.fnmatch(name, pattern):
result.append(os.path.join(root, name))
return result
script_tmp_dir = "/tmp/appiload/appinstall" # Geçici dizin (dosyalar burada ayıklanıyor)
desktopfilea=f"{script_tmp_dir}/squashfs-root/{str(find ('*.desktop', f'{script_tmp_dir}/squashfs-root/')}"
print(desktopfilea)
desktopfile.items()
Result:
/tmp/appiload/appinstall/squashfs-root/['/tmp/appiload/appinstall/squashfs-root/helloworld.desktop']
Use glob.glob instead of writing a function to do this job.
import os, glob
desktopfile = configparser.ConfigParser ()
script_tmp_dir = "/tmp/appiload/appinstall" # Geçici dizin (dosyalar burada ayıklanıyor)
desktopfilea = glob.glob(f'{script_tmp_dir}/squashfs-root/*.desktop')
# desktopfilea = " ".join(desktopfilea) # Join them in one string, using space as seperator
print(str(desktopfilea))
desktopfile.items()
I don't exactly understand what do you mean but I made a simple program that will print all the files with the .desktop extension and save them to 2 files: applications.json in an array and applications.txt just written one after another.
I also have 2 versions of the program, one that will only print and save only the file names with extensions and other one that will print and save the whole path.
File names only:
import os
import json
strApplications = ""
applications = []
for file in os.listdir(os.path.dirname(os.path.realpath(__file__))):
if file.endswith(".desktop"):
applications.append(file)
with open("applications.json", "w") as f:
json.dump(applications, f)
strApplications = strApplications + file + "\n"
with open("applications.txt", "w") as f:
f.write(strApplications)
print(strApplications)
Full file path:
import os
import json
cwd = os.getcwd()
files = [cwd + "\\" + f for f in os.listdir(cwd) if f.endswith(".desktop")]
with open("applications.json", "w") as f:
json.dump(files, f)
with open("applications.txt", "w") as f:
f.write("\n".join(files))
print("\n".join(files))

saving text files to .npy file

I have many text files in a directory with numerical extension(example: signal_data1.9995100000000001,signal_data1.99961 etc)
The content of the files are as given below
signal_data1.9995100000000001
-1.710951390504200198e+00
5.720409824754981720e-01
2.730176313110273423e+00
signal_data1.99961
-6.710951390504200198e+01
2.720409824754981720e-01
6.730176313110273423e+05
I just want to arrange the above files into a single .npy files as
-1.710951390504200198e+00,5.720409824754981720e-01, 2.730176313110273423e+00
-6.710951390504200198e+01,2.720409824754981720e-01, 6.730176313110273423e+05
So, I want to implement the same procedure for many files of a directory.
I tried a loop as follows:
import numpy as np
import glob
for file in glob.glob(./signal_*):
np.savez('data', file)
However, it does not give what I want as depicted above. So here I need help. Thanks in advance.
Here is another way of achieving it:
import os
dirPath = './data/' # folder where you store your data
with os.scandir(dirPath) as entries:
output = ""
for entry in entries: # read each file in your folder
dataFile = open(dirPath + entry.name, "r")
dataLines = dataFile.readlines()
dataFile.close()
for line in dataLines:
output += line.strip() + " " # clear all unnecessary characters & append
output += '\n' # after each file break line
writeFile = open("a.npy", "w") # save it
writeFile.write(output)
writeFile.close()
You can use np.loadtxt() and np.save():
a = np.array([np.loadtxt(f) for f in sorted(glob.glob('./signal_*'))])
np.save('data.npy', a)

Code in python that writes more than one text-file

I have started with a code that is intended to write many textfiles by first reading one textfile. More details of question after the started code.
The textfile (Im reading from texfile called alphabet.txt):
a
b
c
:
d
e
f
:
g
h
i
:
I want the result to be like this:
file1:
a
b
c
file2:
d
e
f
file3:
g
h
i
enter code here
with open('alphabet.txt', 'r') as f:
a = []
for i in f:
i.split(':')
a.append(a)
The code is of course not done. Question: I don't know how to continue with the code. Is it possible to write the textfiles and to place them in a specific folder and too maybe rename them as 'file1, file2...' without hardcoding the naming (directly from the code)?
You could implement that function with something like this
if __name__ == '__main__':
with open('alphabet.txt', 'r') as f:
split_alph = f.read().split(':\n')
for i in range(len(split_alph)):
x = open(f"file_{i}", "w")
x.write(split_alph[i])
x.close()
Depending on whether there is a last : in the alphabet.txt file, you'd have to dismiss the last element in split_alph with
split_alph = f.read().split(':\n')[:-1]
If you got any further questions regarding the solution, please tell me.
file = open("C:/Users/ASUS/Desktop/tutl.py" , "r") # This is input File
text = file.read() # Reading The Content of The File.
file.close() # Closing The File
splitted = text.split(":") # Creating a List ,Containing strings of all Splitted part.
destinationFolder = "path_to_Folder" # Replace this With Your Folder Path
for x in range(len(splitted)): # For loop for each parts
newFile = open(destinationFolder + "/File"+str(x)+".txt" , "w") # Creating a File for a part.
newFile.write(splitted[x]) # Writing the content
newFile.close() # Closing The File

Python3: Index out of range for script that worked before

the attached script returns:
IndexError: list index out of range
for the line starting with values = {line.split (...)
values=dict()
with open(csv) as f:
lines =f.readlines()
values = {line.split(',')[0].strip():line.split(',')[1].strip() for line in lines}
However, I could use it yesterday for doing exactly the same:
replacing certain text in a dir of xml-files with different texts
import os
from distutils.dir_util import copy_tree
drc = 'D:/Spielwiese/00100_Arbeitsverzeichnis'
backup = 'D:/Spielwiese/Backup/'
csv = 'D:/persons1.csv'
copy_tree(drc, backup)
values=dict()
with open(csv) as f:
lines =f.readlines()
values = {line.split(',')[0].strip():line.split(',')[1].strip() for line in lines}
#Getting a list of the full paths of files
for dirpath, dirname, filename in os.walk(drc):
for fname in filename:
#Joining dirpath and filenames
path = os.path.join(dirpath, fname)
#Opening the files for reading only
filedata = open(path,encoding="Latin-1").read()
for k,v in values.items():
filedata=filedata.replace(k,v)
f = open(path, 'w',encoding="Latin-1")
# We are writing the the changes to the files
f.write(filedata)
f.close() #Closing the files
print("In case something went wrong, you can find a backup in " + backup)
I don't see anything weird and I could, as mentioned before use it before ... :-o
Any ideas on how to fix it?
best Wishes,
K

Moving files in python based on file and folder name

Relatively new to python ( not using it everyday ). However I am trying to simplify some things. I basically have Keys which have long names however a subset of the key ( or file name ) has the same sequence of the associated folder.{excuse the indentation, it is properly indented.} I.E
file1 would be: 101010-CDFGH-8271.dat and folder is CDFGH-82
file2 would be: 101010-QWERT-7425.dat and folder is QWERT-74
import os
import glob
import shutil
files = os.listdir("files/location")
dest_1 = os.listdir("dest/location")
for f in files:
file = f[10:21]
for d in dest_1:
dire = d
if file == dire:
shutil.move(file, dest_1)
The code runs with no errors, however nothing moves. Look forward to your reply and chance to learn.
Sorry updated the format.
Try a variation of:
basedir = "dest/location"
for fname in os.listdir("files/location"):
dirname = os.path.join(basedir, fname[10:21])
if os.path.isdir(dirname):
path = os.path.join("files/location", fname)
shutil.move(path, dirname)

Resources