Python Forum
Problem with assigning directory of a file based on cell name
Thread Rating:
  • 2 Vote(s) - 2.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with assigning directory of a file based on cell name
#1
Hi, I wanted to create a script that can opened a text file, c_name.txt where the content of the text file would be list of cell names. After i opened, i need to assign all the cell names as a variable. This is because i need the cell name to open up the next directory which requires the cell name.

#!/usr/bin/python

import os
import os.path

myPWD = os.getcwd()
workspace = "%s/.." %(myPWD)
os.chdir(myPWD)

floc = '/'.join([myPWD, "c_name.txt"])

with open(floc, "r+") as f1:
    cell_names = f1.readlines()

for cell_name in cell_names:
    floc2 = '/'.join([workspace, "library", "ce_ll or ce_ls", "{0}.rpt".format(cell_name)])
    with open(floc2, "r+") as f2:
        for line in f2:
                print line
For floc2, I need to classify if cell name starts with ce_ll, then the report files will be in ce_ls directory whereas if the cell name starts with cell, then the report files will be in cell directory.

Here how it looks like in c_name.txt:
ce_ll_sum
ce_ls_sub
ce_ll_add
ce_ls_clk

Besides, the script located in workspace/test/c_name.txt while the report file is located in workspace/library/$ce_ll or $ce_ls/cell.rpt
Reply
#2
something like:
>>> filelist = []
>>> with open('c_name.txt') as fp:                   
...     for line in fp:                              
...         filelist.append(line.strip())            
...                                                  
>>> filelist                                         
['ce_ll_sum', 'ce_ls_sub', 'ce_ll_add', 'ce_ls_clk'] 
>>>                                                  
Reply
#3
But the problem is, the list of cells in c_name.txt is not just 4, it could be up to 50 cell name and may differ time to time. Is there any way to make it more generic?
Reply
#4
Quote:But the problem is, the list of cells in c_name.txt is not just 4, it could be up to 50 cell name and may differ time to time. Is there any way to make it more generic?

Just replace the list (unless you need it for some other purpose, with running each file in loop, then,
50, 1000, why does it matter. If in a list, it's a simple iteration, in fact a list is not needed:
import os


# Assure in proper directory
os.chdir(os.path.dirname(__file__))

def process_files():
    filelist = []

    with open('c_name.txt') as fp:
        for line in fp:
            pass
            # replace pass with your code for each file here


if __name__ == '__main__':
    process_files()
The added code just makes sure that the source directory is cwd (which is where this script expects data file) you can change that to whatever data directory you wishm but keep it relative to the source.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Copy Paste excel files based on the first letters of the file name Viento 2 419 Feb-07-2024, 12:24 PM
Last Post: Viento
  Color a table cell based on specific text Creepy 11 1,955 Jul-27-2023, 02:48 PM
Last Post: deanhystad
  Using pyinstaller with .ui GUI files - No such file or directory error diver999 3 3,294 Jun-27-2023, 01:17 PM
Last Post: diver999
  Split pdf in pypdf based upon file regex standenman 1 2,071 Feb-03-2023, 12:01 PM
Last Post: SpongeB0B
  Extract file only (without a directory it is in) from ZIPIP tester_V 1 976 Jan-23-2023, 04:56 AM
Last Post: deanhystad
  Problem with print variable in print.cell (fpdf) muconi 0 654 Dec-25-2022, 02:24 PM
Last Post: muconi
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,110 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  no such file or directory in SFTP saisankalpj 2 1,534 Nov-25-2022, 11:07 AM
Last Post: DeaD_EyE
  Deleting rows based on cell value in Excel azizrasul 11 2,613 Oct-19-2022, 02:38 AM
Last Post: azizrasul
Photo Making Zip file of a file and Directory Nasir 2 1,015 Oct-07-2022, 02:01 PM
Last Post: Nasir

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020