Open Files and Folders

Hello Pyrevit Friends :slight_smile:

I would like to open files with pyRevit. Text, Excel, PDF, etc. Also i would like to open folders in the explorer.

I started testing with a .txt file but I´m getting errors with every method i try.
Would appreciate any help :slight_smile:
Kind regards!

import os
import sys

open("C:\\test.txt",'r')

os.startfile("C:\\test.txt")

os.system(r"C:\\test.txt")
IOError: [Errno 2] Die Datei "C:\test.txt" konnte nicht gefunden werden.: C:\test.txt


Script Executor Traceback:
System.IO.FileNotFoundException: Die Datei "C:\test.txt" konnte nicht gefunden werden.
Dateiname: "C:\test.txt"
 bei System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
 bei System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
 bei System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
 bei Microsoft.Scripting.PlatformAdaptationLayer.OpenInputFileStream(String path, FileMode mode, FileAccess access, FileShare share)
 bei IronPython.Runtime.PythonFile.__init__(CodeContext context, String name, String mode, Int32 buffering)
 bei IronPython.Modules.Builtin.open(CodeContext context, String name, String mode, Int32 buffering)
 bei Microsoft.Scripting.Interpreter.FuncCallInstruction`5.Run(InterpretedFrame frame)
 bei Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
 bei Microsoft.Scripting.Interpreter.LightLambda.Run5[T0,T1,T2,T3,T4,TRet](T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4)
 bei System.Dynamic.UpdateDelegates.UpdateAndExecute4[T0,T1,T2,T3,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2, T3 arg3)
 bei Microsoft.Scripting.Interpreter.DynamicInstruction`5.Run(InterpretedFrame frame)
 bei Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
 bei Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,TRet](T0 arg0, T1 arg1)
 bei IronPython.Compiler.PythonScriptCode.RunWorker(CodeContext ctx)
 bei PyRevitLabs.PyRevit.Runtime.IronPythonEngine.Execute(ScriptRuntime& runtime)

hi, @Gerhard.P
It may be an issue with write access on the C drive.

  • The open() is mostly use to read and write data in a file see: Python File Open
  • the other two are different ways to open a file. , os.startfile() would be the way to go

why not going wit a urlbutton and put this in your bundle.yaml file?

you may have to restart Revit after creating such a button _ the UI did not properly refresh upon reload for some reason.

Hello @Jean-Marc ,

I think you are right, tried it at work and it works here.
os.startfile is all it needs.

import os

os.startfile("C:\\Users\\......xlsx")

And here is the code for opening a folder and creating a folder.

import subprocess
subprocess.Popen('explorer "C:\\Users\\...\\Folder"')

import os
os.mkdir("C:\\Users\\...\\newfolder")
1 Like

opening directories and creating directories is also integrated into the pyRevit core utils:

pyrevit.coreutils. open_folder_in_explorer (folder_path )

Open given folder in Windows Explorer.

Parameters: folder_path (str) – directory path

pyrevit.coreutils. verify_directory (folder )

Check if the folder exists and if not create the folder.

Parameters: folder (str) – path of folder to verify
Returns: path of verified folder, equals to provided folder
Return type: str
Raises: OSError on folder creation error.
1 Like

I’m trying to create folder list form that include my files in folder path. Can you explain more about your solution? thanks

I’m trying to open a pdf as well. but for some reason is not working.
Any idea?