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*
Related
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.
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 3 years ago.
Improve this question
I want to find the current working directory (cwd) with os library and write .txt file on it.
Something like this:
import os
data=["somedatahere"]
#get_the_current_directory
#if this_is_the_current_directory:
new_file=open("a_data.txt", "a")
new_file.write(data)
new_file.close()
It can be done using the os library, but the new pathlib is more convenient if you are using Python 3.4 or later:
import pathlib
data_filename = pathlib.Path(__file__).with_name('a_data.txt')
with open(data_filename, 'a') as file_handle:
file_handle.write('Hello, world\n')
Basically, the with_name function said, "same dir with the script, but with this name".
import os
You're halfway there!
The rest of it is:
print(os.getcwd())
Of course, you don't need to know that value,
as a_data.txt or ./a_data.txt suffices.
As a side note, you'd be better off closing with a with handler:
with open('a_data.txt', 'a') as new_file:
new_file.write(data)
Habitually using a Resource Manager means
never having to say, "sorry, forgot to close it!"
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)))
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'm trying to search a file in my directory files. I saw an example and couldn't understand:
import os
import glob
file_glob = './rpts/folderA/*_fileA.txt'
magic_check = re.compile('[*?[]')
if(magic_check.search(file_glob))
file_list = glob.glob(os.path.expanduser(file_glob))
What does the ./ part mean? I understand that ../ is switch to previous dir.
What I think it does:
expand the wild card to get a list of files that matches the regex
The files are stored in a list called file_list
Magic check regex, [*?[]: What is the [ inside [ ] for?
As Martijn said, this is UNIX shell notation for the current location (cwd or pwd). The reason it's in the command is to be more robust. If the user's environment doesn't have "./" in the search path ($PATH variable), then the shell won't find the file rpts/folderA/*_fileA.txt. With "./" at the front, this script is independent of $PATH.
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 7 years ago.
Improve this question
I need to find a specified folder which can be located anywhere on the disk.
I wonder what would be the fastest solution to do this with a Python 3.4.
I know folder name, for example "XXX" and it's subfolder "YYY". And for not to be easy, there are many folders called "XXX" but none of them contains "YYY" folder. So it's quite unique.
I wanted to walk on C: and find folder "XXX" and if it is found then check if it contains "YYY".
But maybe there is some kind of library which can speed this up or something?
import os
partition = input("Which drive do you want to search? ")
dirname = "XXX"
subdirname = "YYY"
for root, dirs, _fnames in os.walk(partition):
if os.path.basename(root) != dirname: continue
if not os.path.isdir(os.path.join(root, subdirname)): continue
print("Found required folder:", root)
break