def crawl_unc_helper(client, uncpath, patterns, options): records = client.get_unc_listing(uncpath) for record in records: if record['IsFolder'] == '1': if record['LinkId'] == uncpath: continue crawl_unc_helper(client, record['LinkId'], patterns, options) else: for pattern in patterns: if pattern.lower() in record['LinkId'].lower(): if options.download: try: data = client.get_unc_file(record['LinkId']) except TypeError: pass else: winpath = PureWindowsPath(record['LinkId']) posixpath = Path(winpath.as_posix() ) # Windows path to POSIX path posixpath = Path( *posixpath.parts[1:]) # get rid of leading "/" dirpath = posixpath.parent newdirpath = mkdir_p(dirpath) filename = str(newdirpath / posixpath.name) try: with open(filename, 'w') as fd: fd.write(data) # If path name becomes too long when filename is added except IOError as e: if e.errno == errno.ENAMETOOLONG: rootpath = Path(newdirpath.parts[0]) extname = posixpath.suffix # Generate random name for the file and put it in the root share directory filename = ''.join( choice(ascii_uppercase + digits) for _ in range(8)) + extname filename = str(rootpath / filename) with open(filename, 'w') as fd: fd.write(data) warning( 'File %s"%s"%s was renamed and written to %s"%s"%s' % (M, str(posixpath), S, M, filename, S)) else: raise else: if dirpath != newdirpath: warning( 'File %s"%s"%s was written to %s"%s"%s' % (M, str(posixpath), S, M, filename, S)) list_unc_helper(client, record['LinkId'], options, show_parent=False) break
def posixify_path(path: str) -> str: """Ensure that a path is in POSIX format. Windows paths are converted to POSIX style, where the "Drive" is listed as the first folder (e.g., ``/c/Users/globus_user/``). Arguments: path (str): Input path Returns: str: Rectified path """ is_windows = re.match('[A-Z]:\\\\', path) is not None if is_windows: ppath = PureWindowsPath(path) return '/{0}{1}'.format(ppath.drive[:1].lower(), ppath.as_posix()[2:]) return path # Nothing to do for POSIX paths
def main(args): import argparse parser = argparse.ArgumentParser(description='Because Windows is the worst.', prog=args[0]) parser.add_argument('path', help='the path to operate on' ) parser.add_argument('--from-windows', action='store_true', help='interpret source as a Windows path (default: POSIX path)' ) parser.add_argument('--to-windows', action='store_true', help='output a Windows path (default: POSIX path)' ) parser.add_argument('--escape-backslashes', action='store_true', help='escape any backslashes in the output' ) args = parser.parse_args(args[1:]) if args.from_windows: path = PureWindowsPath(args.path) else: path = PurePosixPath(args.path) if args.to_windows: out = str(PureWindowsPath(path)) else: out = path.as_posix() if args.escape_backslashes: out = out.replace('\\', '\\\\') sys.stdout.write(out)
def getWindowsHomeDirectory(): winPath = PureWindowsPath(check_output(["cmd.exe", "/C", 'echo %USERPROFILE%']).decode().strip()) return PurePosixPath(winPath.as_posix())
N_CLASSES = 6 N_SECTIONS = 15 VAL_FRAC = 0.2 TEST_FRAC = 0.2 #with open(infile) as csvfile: # data_reader = csv.reader(csvfile,delimiter='\t') data = pd.read_csv(infile, sep='\t') fnames = data.iloc[:, 5] start_idx = 7 f_idx = 0 data_out = [] for f in fnames: fwp = PureWindowsPath(f) file_base = os.path.splitext(fwp.as_posix())[0] for i in range(N_SECTIONS): this_file = "./image_sections/" + file_base + "_" + str(i) + ".jpg" this_data = [] for j in range(N_CLASSES): this_idx = start_idx + i + j * (N_SECTIONS + 1) this_data.append(data.iloc[f_idx, this_idx]) data_out.append([this_file, this_data]) f_idx = f_idx + 1 #test #data_out.append(['test',[0, 0, 0, 0, 0, 0]]) #print out data ftrain = open(train_file, 'w')
def __init__(self, resource): config_mimetype_guess = config.get('ckan.mimetype_guess', 'file_ext') self.filename = None self.mimetype = None raw_link_path = resource.get('link_path', resource.get('url', '')) if raw_link_path.startswith("\\"): link_path = PureWindowsPath(raw_link_path) else: link_path = PurePath(raw_link_path) self.mount_path = None self.clear = resource.pop('clear_upload', None) self.filesize = 0 # bytes self.filename = munge.munge_filename(link_path.name) self.link_path = link_path.as_posix() # Construct mountpoint dictionary mountpoints = dict( zip( shlex.split( config.get('ckanext.linked_storage.net_paths', None)), shlex.split( config.get('ckanext.linked_storage.mountpoints', None)))) log.debug('Available mountpoints: %r', mountpoints) for m in mountpoints: if self.link_path.upper().startswith(m.upper()): self.mount_path = mountpoints[m] + self.link_path[len(m):] if not self.mount_path: raise ValidationError( 'Unable to locate file via the known mount points') resource['url_type'] = 'upload' # appear to CKAN as an upload resource[ 'url'] = self.filename # use filename just like an uploaded file resource['link_path'] = self.link_path resource['last_modified'] = datetime.datetime.fromtimestamp( os.path.getmtime(self.mount_path)) with open(self.mount_path, 'rb') as f: f.seek(0, os.SEEK_END) self.filesize = f.tell() # go back to the beginning of the file buffer f.seek(0, os.SEEK_SET) # check if the mimetype failed from guessing with the url if not self.mimetype and config_mimetype_guess == 'file_ext': self.mimetype = mimetypes.guess_type(self.filename)[0] if not self.mimetype and config_mimetype_guess == 'file_contents': try: self.mimetype = magic.from_buffer(f.read(), mime=True) f.seek(0, os.SEEK_SET) except IOError as e: # Not that important if call above fails self.mimetype = None
f'Error opening file: {str(error)}. Applying default renaming...') newFilename = f'{leadingText}_{seqNo}{fileExt}' finally: # Return the new filename. return newFilename # MAIN if __name__ == '__main__': ''' INPUT - WINDOWS DIRECTORY ''' # Directory containing the images to rename - CHANGE AS REQUIRED windowsPath = PureWindowsPath( r"/PATH/TO/FOLDER/") # Required on Windows platforms. IMAGE_DIR = Path(windowsPath.as_posix()) # Loop through each item in the directory and rename the item if it is a file. for i, imageFile in enumerate(os.listdir(IMAGE_DIR)): imageFilePath = IMAGE_DIR / imageFile # If the current item is a directory then pass. if (imageFilePath).is_dir(): continue print(f'Image {i+1} - ', end=' ') # Get a new filename for the current image newFilename = getNewFilename(imageFilePath, i + 1) # Calculate the new file path for the new file name
# Retorna uma string de um caminho no estilo posix. # Ou seja, com a barra "/" para direita. from pathlib import PureWindowsPath, Path diretorio = PureWindowsPath('c:\Documentos\Recentes') print(diretorio.as_posix()) # c:/Documentos/Recentes # Retorna um caminho como a URI de um arquivo. # Se o caminho não for absoluto, será lançada 'ValueError'. try: arquivo = Path('/usr/bin/teste.tar.gz') print(arquivo.as_uri()) # file:///usr/bin/teste.tar.gz arquivo = Path('.') print(arquivo.as_uri()) # Exceção lançada. except ValueError as erro: print('O caminho não é absoluto.')
#if no quarantine database exists, create one if not Path(local_db).exists(): build_local_db(local_db) #build virus signature database infected_df = build_virus_db(token, virus_db, local_db, seconds) #TODO for testing only, remove #print("Infected Files: ") #print(infected_df) #create list of paths to be mounted paths = [] for index, row in infected_df.iterrows(): comp = PureWindowsPath(row['computerId_name']).stem local_path = PureWindowsPath(row['pathName']) path = '//' + comp + '/' + local_path.as_posix().replace(":", "$") paths.append((path, comp)) #TODO for testing only, remove #print(paths) #paths = [] #TODO For testing only, remove #paths = [("//ITS-172099/c$/users/BradshJ1/desktop", "ITS-172099"), ('//ITS-172099/c$/users/BradshJ1/pictures/test', "ITS-172099")] #mount paths and run scan for path in paths: try: #make sub directory for each computer sub_quarantine = quarantine + '/' + path[1] comp = InfectedComputer(virus_db, sub_quarantine, log_file, local_db,
from bs4 import BeautifulSoup import sqlite3 #------------------------------------------------------------------------- # lecture du fichier contenant les données working_dir = PureWindowsPath(os.getcwd()) data_dir = PureWindowsPath(working_dir.joinpath('Data')) data_file = PureWindowsPath(data_dir.joinpath('module_2_data_1.xls')) data_result_name = 'result.csv' # lecture des deux premières colonnes de la seconde feuille de calcul du fichier excel if 0: df = pd.read_excel(data_file.as_posix()) df = pd.read_excel(data_file.as_posix(), sheet_name='Sheet2', usecols=[0, 1]) #------------------------------------------------------------------------- # web scrapping # exraction du text contenu dans les balises h2 #------------------------------------------------------------------------- if 0: page = r.get('http://time.com/4572079/best-inventions-2016/') soup = BeautifulSoup(page.text, 'html.parser') scrapted_titles = []
# Compile a dictionary of {course_code: course_name} courses = {} # Compile a dictionary of {course_id: course_name} course_ids = {} # For each course, add the course code - course name into the dictionary only if the enrollment_term_id is 289 (current semester - Sem 1 2020) for course in courses_JSON: if course['enrollment_term_id'] != 311: continue courses[course['course_code']] = course['name'] course_ids[course['id']] = course['name'] #pprint(courses) #pprint(course_ids) # Create folders for each course print('Creating course folders...') # Create a folder to store the course folders in windowsPath = PureWindowsPath(r"D:/OneDrive - RMIT University/2020 Semester 2") UNI_DIR = Path(windowsPath.as_posix()) os.makedirs(UNI_DIR,exist_ok='true') # For each course, create 'Lecture', 'Assignments' and 'Labs' folders for courseCode in courses.keys(): # Course Code - Course Name folder folder_name = '{} - {}'.format(courseCode,courses[courseCode]) os.makedirs(UNI_DIR/folder_name/'Lectures',exist_ok='true') os.makedirs(UNI_DIR/folder_name/'Assignments',exist_ok='true') os.makedirs(UNI_DIR/folder_name/'Labs',exist_ok='true')
import numpy as np import pandas as pd #---- charger les données try: xmlFileName = 'O:\\Dupont\\24 BenAdmin-Param\\05 Paiement\\SEPA\\5_adresse_paiement_correction_is_iban.xlsx' currentDir = PureWindowsPath(os.getcwd()) sampleDir = PureWindowsPath(currentDir.joinpath('WorldsTallestMountains')) dataFile = PureWindowsPath(sampleDir.joinpath('Mountains.csv')) data = pd.DataFrame() data = pd.read_csv(dataFile.as_posix()) dataxml = pd.read_excel(xmlFileName) # clean up des données data.set_index('Mountain', inplace=True) data.drop(['Rank', 'Height (ft)', 'Coordinates', 'Parent mountain'], axis=1, inplace=True) data.drop(['Mount Everest / Sagarmatha / Chomolungma', 'Muztagh Ata'], axis=0, inplace=True) # supprimer toutes les lignes qui contiennent des valeurs non définies
#文化局決算 from pathlib import Path, PureWindowsPath from aisimporter import import_cba_csv dir = PureWindowsPath("C:\\Users\\fhopecc\\Google 雲端硬碟\\家教中心決算\\dataset") print(dir.as_posix()) print(dir / "cba.db") import_cba_csv(str(dir.as_posix()), str((dir / "cba.db").as_posix()))
a2.legend(labels=df_hap_10.columns[3:10], loc="upper right", bbox_to_anchor=(1.2, 1)) a2.set_xlabel('Country') a2.set_ylabel('Happiness Score') a2.set_title(title) plt.show() #------------------------------------------------------------------------- # lecture du fichier contenant les données du World working_dir = PureWindowsPath(os.getcwd()) data_dir = PureWindowsPath(working_dir.joinpath('Data')) data_file = PureWindowsPath(data_dir.joinpath('data.csv')) df = pd.read_csv(data_file.as_posix()) df.info() df.columns df.dtypes df.count() df.dropna(inplace=True) df.count() # selection des colonnes de type numérique don on produit une copie df_num_col = df.select_dtypes(include=['float64', 'int64']).copy() # produit une série somme / max / min des valeurs prises par colonne sum_num_col = df_num_col.sum()
os.remove('file.log') os.remove('file.markdown.lua') os.remove('file.markdown.out') os.remove('file.out') os.remove('file.tex') os.remove('file.toc') except OSError: pass # print(escape_latex('\nAlso some crazy characters: $&#{}')) # print(escape_latex('\nAlso some crazy characters: \qualquer{} $&#{}')) from pathlib import Path, PureWindowsPath cwd = os.getcwd() topwd = PureWindowsPath(cwd).parent.parent topwd = topwd.as_posix() modelos = topwd + '/modelos' fontes = topwd + '/fontes' ufontes = (topwd + u'/fontes').encode('utf-8') saidas = topwd + '/pdfs' main_tex = topwd + '/main/main.tex' path = fontes.encode('utf-8').strip() debug = 0 passagens = 2 mobonly = 0 pconly = 0 last = 0 abrir = 0