㈠ 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()