Excecute python script to all excel files in a directory [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I was wondering if it could be possible to get access to all the files in a directory from a python script.
The script is the following:
import pandas as pd
import numpy as np
#[4] main_folder=r'C:/Users/MainUser/Desktop/sediment_processing/raw_data/cohesive/'
#[6] section_name = 'UHS_TIG01_PAC07032019'
#[7] file_extension = '.xlsm'
file = main_folder + section_name + file_extension
excel = pd.read_excel(file,sheet_name='ASCII Data',header=3)
df = excel.iloc[1:,21:53]
#And more lines for calculations
What I would like to know is if it's possible to, inside this code, write something instead of lines [4], [6] and [7] that could let apply all the calculations in this code to all the excel files in my directory:
C:/Users/MainUser/Desktop/sediment_processing/raw_data/cohesive/.

You can do the following:
import os
folder = '/your/folder/location/'
files = [os.path.join(folder, item) for item in os.listdir(folder)]
This will get you a list of all files in the folder and then you can do whatever you want with them.

Related

Iterating through images in folder path [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
If I have:
flags = ['australia.png', 'canada.png', 'newzealand.png', 'uk.png', 'usa.png']
and if these images are in some folder called "flags", how can I set the path to that folder to iterate through them?
You need to include the path to the flags folder which can be done using the os module. This will be more robust and portable than adding them as strings.
import os
flag_folder = '/path/to/flags'
flags = ['australia.png', 'canada.png', 'newzealand.png', 'uk.png', 'usa.png']
for filename in flags:
flag_path = os.path.join(flag_folder, filename)
# do something with flag_path
In case you want something to happen just when the images are in the flags folder:
import os
path = '.../flags/'
for flag in flags:
if os.path.exists(path + flag):
*do something*

how to get a file path with the name and the kind of the file using python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
so I am getting a name of and I know it's an EXE file.
I need to get the path which the file is at.
It could be anywhere in the computer.
Is there a muddle for it? or a simple script that I am missing?
import os
files = os.listdir(path='.') # getting all files in currect directory
def get_path(files):
filename = ""
for file in files:
if file == "main.py": # replace "filename.exe" with the name of the file
filename = file # saving the name of the file in a variable
break
elif re.match(r"^\.", file):
get_path(os.listdir(file))
absolute_path = os.path.abspath(filename) # absolute path to the file
return absolute_path
print(get_path(files))
You can try to use os.walk()
import os
file_name = 'the_file_to_find.exe'
for root, _, files in os.walk('\\'):
if file_name in files:
print(os.path.join(root, file_name))
break
However, searching in the whole '\' can be slow, so it would be better to be more precise.

How to create a for loop to create dataframes in Python? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have 732 .txt files in a folder.
I want to make a unique Pandas dataframe for each of them, but without doing it manually one by one.
Is there a way to open all the files in Python and to use a for loop to create dateframes for each one? Could someone provide me a starting code example?
Thank you!
It sounds like you're wanting to use Pandas to read a bunch of CSVs. If they're all in the same directory and nothing else with a .txt extension is in there, you can use
import glob
files = glob.glob('./*.txt')
to get the list of relevant files.
Then you can use a list comprehension to get a list of dataframes:
import pandas as pd
dfs = [pd.read_csv(f) for f in files]
with whatever params you need for read_csv in there.
The following pseudocode should open all the .txt files in any given directory and help you build a dataframe for each. It does not use glob because glob is slow:
import os
dir = '/where/your/txts/are/'
for filename in os.listdir(dir):
if filename.endswith('.txt'):
content = open(dir+filename, 'r').read()
dataframe = build_your_dataframe(content)

How to define as Save As Function in Python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
hello I have below code like this, I would like to do save as same name but add a Tag New to same file name so save it as new_TBM. Any help is appreciated.
import sys
from tableaudocumentapi import Workbook
sourceWB = Workbook('C:\\Users\\rmakkena\\Music\\TBM.twb')
sourceWB.datasources[0].connections[0].server = "MY-NEW-SERVER"
sourceWB.datasources[0].connections[0].dbname = "NEW-DATABASE"
sourceWB.datasources[0].connections[0].username = "rithesh"
sourceWB.save_as()
You only need to do a bit of processing on your (basename part of the) filename. For that, we use [Python]: os.path — Common pathname manipulations. How it's done:
Split the file name into:
dirname (directory tree path: C:\Users\rmakkena\Music) and
basename (plain file name: TBM.twb)
Add the "new_" prefix to the basename
Rejoin the 2 parts together (C:\Users\rmakkena\Music\new_TBM.twb)
The (slightly modified) code (check [GitHub]: document-api-python/tableaudocumentapi/workbook.py: def save_as(self, new_filename):):
import sys
import os
from tableaudocumentapi import Workbook
file_name = "C:\\Users\\rmakkena\\Music\\TBM.twb"
new_tag = "new_"
sourceWB = Workbook(file_name)
sourceWB.datasources[0].connections[0].server = "MY-NEW-SERVER"
sourceWB.datasources[0].connections[0].dbname = "NEW-DATABASE"
sourceWB.datasources[0].connections[0].username = "rithesh"
sourceWB.save_as(os.path.join(os.path.dirname(file_name), new_tag + os.path.basename(file_name)))

Extracting all the files of a selected extension from a zipped file [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am new to python, and I wanted to extract three single files of a different extensions each from a zipped file. I don't know their filenames, just their extensions.
Let's say that the following format exists:
---ZippedDirectory.zip
|_
RandomnameFile1.KnownFormat1
|_
RandomnameFile2.KnownFormat2
|_
RandomnameFile3.KnownFormat3
|...
I need to extract the above files, I only know the formats. There might be other files in this zipped archive.
I am confused as to how to achieve this, Any help would be awesome!
Thanks!
you should be able to do something like this
import zipfile
def main():
archive = 'archive.zip'
directory = './'
extensions = ('.txt', '.pdf')
zip_file = zipfile.ZipFile(archive, 'r')
[zip_file.extract(file, directory) for file in zip_file.namelist() if file.endswith(extensions)]
zip_file.close()
if __name__ == '__main__':
main()
Mine is a simpler version of Jones'. Works for just one extension.
from zipfile import ZipFile
with ZipFile(r'C:\scratch\folder to process\try.zip') as theZip:
fileNames = theZip.namelist()
for fileName in fileNames:
if fileName.endswith('py'):
content = theZip.open(fileName).read()
open(fileName, 'wb').write(content)

Categories

Resources