㈠ ASP根據access中存儲的文件路徑名與文件名 打開文件
<%sql1="select * from img where newsid="&id
set rs1=Server.CreateObject("ADODB.RecordSet")
Rs1.open sql1,conn,1,1
if Rs1.eof and Rs1.bof then
response.write "沒有文件"
else
for i=1 to rs1.RecordCount%>
附件<%=i%>下載:<a href="files\<%=rs1("filename"悶遲)%>">
<%=rs1("filename")%></a>
<%rs1.movenext()
next
end if%>
如果是txt型的以上野罩卜代碼就是在瀏覽器中直接顯示的,也可以下載,要看瀏覽器的設置了。
如果是別的後綴名,我不敢確定,沒試頌穗過。
㈡ C#根據文件名怎麼獲得當前路徑(不要文件名)
1,如果你是指WEB程序里的頁面文件名,那麼可以這樣:
var physicalPath = Server.MapPath(文件虛擬路徑); 這樣專可以屬獲得物理路徑
var dir = System.IO.Path.GetDirectoryName(physicalPath); 可以獲得不帶文件名的路徑
2,如果你是指非WEB程序,那就要知道你所指的文件名,包不包含路徑關系了,如包含,可以按照上面的步驟2
㈢ python怎麼根據文件名獲得文件路徑
獲取文件路徑可以使用OS模塊,代碼如下:
importos
srcPath=r"WallReference.pdf"
path=os.path.abspath(srcPath)
print"全路徑為:",path
print"路徑名,文件內名",os.path.split(path)
導入容os模塊;
需要操作的文件;
獲取文件的全路徑,即帶盤符的路徑;
輸出全路徑;
輸出路徑名和文件名;
㈣ 如何在C#中獲取文件名與文件路徑
使用Directory.EnumerateFiles(String, String, SearchOption)方法可搜索所有目錄以按指定路徑行肆耐獲取與指定搜索模式匹配的文件名稱的列雹友表。
using System;
using System.IO;
using System.Linq;
class Program
{
static void Main(string[] args)
{
try
{
var files = from file in Directory.EnumerateFiles(@"c:\", "*.txt", SearchOption.AllDirectories)
from line in File.ReadLines(file)
where line.Contains("Microsoft"檔春)
select new
{
File = file,
Line = line
};
foreach (var f in files)
{
Console.WriteLine("{0}\t{1}", f.File, f.Line);
}
Console.WriteLine("{0} files found.", files.Count().ToString());
}
catch (UnauthorizedAccessException UAEx)
{
Console.WriteLine(UAEx.Message);
}
catch (PathTooLongException PathEx)
{
Console.WriteLine(PathEx.Message);
}
}
}
㈤ python中如何根據文件名找他的路徑。現在我遍歷到了想要的文件,但是操作的時候需要根據路徑提取。
遍歷用os.walk:
importos
fromos.pathimportjoin,getsize
forroot,dirs,filesinos.walk('python/Lib/email'):
printroot,"consumes",
printsum(getsize(join(root,name))fornameinfiles),
print"bytesin",len(files),"non-directoryfiles"
if'CVS'indirs:
dirs.remove('CVS')#don'tvisitCVSdirectories
㈥ java怎麼根據文件名獲取文件絕對路徑
可以通過「 類名.class.getResource("").getPath()」方法實現。
舉例:」String path = XMLS.class.getResource("").getPath()「.
解釋:以上語句就是獲取到XMLS編譯後的絕對路徑(無法獲取到java文件路徑的,因為java運行的都是class文件)。
㈦ 根據文件的全路徑獲取文件簡單名稱,如:fileFullName="F:\a.txt" 則返回a.txt (C/C++ 實現) 要考慮文件
LZ知道CString類中有成員函數GetLength(),和ReverseFind ()以及Right()函數吧!
先調用GetLength得到總字元串的長度,再用ReverseFind函數得到\在字元串中的位置,然後用Right函數獲取就行了,具體的實現LZ看看這幾個函數的使用方法就知道了(參考MSDN)
void CCounterDlg::OnBnClickedButton2()
{
// TODO: 在此添加控制項通知處理程序代碼
UpdateData();
if(!m_fileName);//m_fileName為編輯控制項關聯的一個CString類型的變數
else
{
int i=m_fileName.ReverseFind('\\');//對『\』轉義
int j=m_fileName.GetLength();
AfxMessageBox(m_fileName.Right(c-t-1));
}
}
㈧ Python中,已知文件路徑,但並不知道文件名。如何用Python實現文件名的獲取
import os
d=文件來夾自路徑
l=os.listdir(d)
則l就是文件夾里所以子文件夾和文件的列表,可以通過os.path.isfile和os.path.isdir來對列表裡的項進行判斷看是文件還是文件夾。
㈨ java根據路徑讀取文件
直接貼代碼吧。不過這里要做一個簡單的說明,對於這個程序,我們必須保證我們在C盤下有一個Users\HP\Desktop的文件夾,因為在後面寫入文件的時候,如果路徑中的文件不存在,是程序可以自動為其添加,但如果沒有了這個路徑,則程序會報找不到文件路徑的異常。你可以對這個異常進行人性的處理,還可以在程序要向這個路徑寫入數據之前,創建出這個路徑。
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;
public class ListRoots {
private static final String LOG_BASE_PATH = "C:\\Users\HP\\Desktop\\";
private static ArrayList<String> mfiles = new ArrayList<String>();
/**
* 得到給定路徑下的目錄或是文件
* @param strPath
* @throws Exception
*/
private static void displayDirsOrFiles(String strPath) throws Exception {
try {
File f = new File(strPath);
if (f.isDirectory()) {
File[] fList = f.listFiles();
for (int j = 0; j < fList.length; j++) {
if (fList[j].isDirectory()) {
System.out.println("Directory is: " + fList[j].getPath());
displayDirsOrFiles(fList[j].getPath()); // 對當前目錄下仍是目錄的路徑進行遍歷
}
}
for (int j = 0; j < fList.length; j++) {
if (fList[j].isFile()) {
String name = fList[j].getPath().toString();
System.out.println("Filename is: " + name);
mfiles.add(fList[j].getPath());
}
}
}
} catch (Exception e) {
System.err.println("Error: " + e);
}
}
/**
* 向文件中寫入數據
* @param dirOrfiles
* @throws IOException
*/
private static void writeDetailToFiles(ArrayList<String> dirOrfiles) throws IOException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd H:m:s");
toFiles(getLogPath(), format.format(new Date()) + " -- 檢測到文件" + dirOrfiles.size() + "個:" + "\r\n");
for (String file : dirOrfiles) {
toFiles(getLogPath(), file + "\r\n");
}
toFiles(getLogPath(), "--------------------------------------------------------------------------------------------------------------------------\r\n");
}
/**
* 獲得寫入數據的路徑
* @return
*/
private static String getLogPath() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
return LOG_BASE_PATH + format.format(new Date()) + ".txt";
}
/**
* 向dir路徑下寫入數據data
* @param path
* @param data
*/
private static void toFiles(String path, String data) throws IOException {
File file = new File(path);
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file, true);
fw.write(data);
fw.flush();
fw.close();
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("請輸入待遍歷目錄路徑(Format: F:\\a\\b):");
String strPath = input.nextLine();
try {
displayDirsOrFiles(strPath.replace("\\", "\\\\"));
writeDetailToFiles(mfiles);
} catch (Exception e) {
e.printStackTrace();
}
}
}
㈩ java 根據文件獲取文件名及路徑的方法
通過File類獲取文件,然後通過以下兩種方法獲取絕對路徑和名稱。返回類型為String
獲取絕對路徑:file.getAbsolutePath()
獲取名稱: file.getName()