0
votes

i need to get all folder names EXCEPT for "Archives" using Path() ONLY as i need to use glob later in the for loop. Im on Kali Linux and the file structure is ./sheets/ and then the folders Archives, and Test(ALSO NOTHINGS INSIDE) with files creds.json and sheets.py.

# Imports
from pathlib import Path
import pandas as pd
import pygsheets
import glob
import os

# Setup
gc = pygsheets.authorize(service_file='./creds.json')
email = str(input("Enter email to share sheet: "))
folderName = Path("./") # <<<<<<<<<<<<< HERE IS PROBLEM

for file in folderName.glob("*.txt"):
    if not Path("./Archives").exists():
        os.mkdir("./Archives")

    df = pd.DataFrame()
    df['name'] = ['John', 'Steve', 'Sarah', 'YESSSS']
    gc.create(folderName)
    sh = gc.open(file)
    sh.share(email, role='writer', type='user')
    wks = sh[0]
    wks.set_dataframe(df,(1,1))

i expect the output to the variable folderName be any folder name except Archives as a string. my goal is a script that when run, gets the folder name in ./sheets/ (Test in this case) as the newly created spreadsheet's name, get file names as headers, and stuff in files (seperated by newlines) as the stuff underneath the header(file names) then shares the sheet with me at my email. using pygsheets by the way

1

1 Answers

0
votes
from pathlib import Path

p = Path('./sheets')

# create Archives under p if needed
archives = p / 'Archives'
if not archives.exists(): archives.mkdir()

# find all directories under p that don't include 'Archives'
folder_dirs = [x for x in p.glob('**/*') if x.is_dir() and 'Archives' not in x.parts]

# find all *.txt* files under p that don't include 'Archives'
txt_file_dirs = [x for x in p.glob('**/*.txt') if x.is_file() and 'Archives' not in x.parts]

for file in txt_file_dirs:
    file_name = file.stem

When using pathlib you will be working with objects, not strings and there are many methods for working on those objects, within the library.