Python os.path.isfile抱怨映射的网络驱动器中的文件不存在

时间:2019-03-26 12:24:34

标签: python network-drive

在本地网络中共享的文件夹被映射到Windows 10中的网络驱动器(字母V:)。以下python行检查文件是否在映射的网络驱动器中:

import os
print(os.path.isfile("V:\\FORENSOR DTB Dataset processed\\pow_day_125236_07.5.avi"))

这将返回false。文件夹中的任何文件都会发生相同的情况。 copyfile()中的shutils还会引发错误,指出文件不存在。但是该文件在文件夹中。这是什么问题?

在建议/链接的答案之后,我尝试了以下所有操作(V:是驱动器号,DARAS-NAS是网络位置的名称):

print(os.path.isfile(r'\\DARAS-NAS\FORENSOR DTB Dataset processed\pow_day_125236_07.5.avi'))
print(os.path.isfile(r'\\DARAS-NAS\\FORENSOR DTB Dataset processed\\pow_day_125236_07.5.avi'))
print(os.path.isfile(r'\\\\DARAS-NAS\FORENSOR DTB Dataset processed\pow_day_125236_07.5.avi'))
print(os.path.isfile(r'\\\\DARAS-NAS\\FORENSOR DTB Dataset processed\\pow_day_125236_07.5.avi'))
print(os.path.isfile(r'\DARAS-NAS\FORENSOR DTB Dataset processed\pow_day_125236_07.5.avi'))
print(os.path.isfile(r'/DARAS-NAS/FORENSOR DTB Dataset processed/pow_day_125236_07.5.avi'))
print(os.path.isfile(r'//DARAS-NAS/FORENSOR DTB Dataset processed/pow_day_125236_07.5.avi'))

print(os.path.isfile(r"\\DARAS-NAS\FORENSOR DTB Dataset processed\pow_day_125236_07.5.avi"))
print(os.path.isfile(r"\\DARAS-NAS\\FORENSOR DTB Dataset processed\\pow_day_125236_07.5.avi"))
print(os.path.isfile(r"\\\\DARAS-NAS\FORENSOR DTB Dataset processed\pow_day_125236_07.5.avi"))
print(os.path.isfile(r"\\\\DARAS-NAS\\FORENSOR DTB Dataset processed\\pow_day_125236_07.5.avi"))
print(os.path.isfile(r"\DARAS-NAS\FORENSOR DTB Dataset processed\pow_day_125236_07.5.avi"))
print(os.path.isfile(r"/DARAS-NAS/FORENSOR DTB Dataset processed/pow_day_125236_07.5.avi"))
print(os.path.isfile(r"//DARAS-NAS/FORENSOR DTB Dataset processed/pow_day_125236_07.5.avi"))

print(os.path.isfile('\\DARAS-NAS\FORENSOR DTB Dataset processed\pow_day_125236_07.5.avi'))
print(os.path.isfile('\\DARAS-NAS\\FORENSOR DTB Dataset processed\\pow_day_125236_07.5.avi'))
print(os.path.isfile('\\\\DARAS-NAS\FORENSOR DTB Dataset processed\pow_day_125236_07.5.avi'))
print(os.path.isfile('\\\\DARAS-NAS\\FORENSOR DTB Dataset processed\\pow_day_125236_07.5.avi'))
print(os.path.isfile('\DARAS-NAS\FORENSOR DTB Dataset processed\pow_day_125236_07.5.avi'))
print(os.path.isfile('/DARAS-NAS/FORENSOR DTB Dataset processed/pow_day_125236_07.5.avi'))
print(os.path.isfile('//DARAS-NAS/FORENSOR DTB Dataset processed/pow_day_125236_07.5.avi'))

print(os.path.isfile("\\DARAS-NAS\FORENSOR DTB Dataset processed\pow_day_125236_07.5.avi"))
print(os.path.isfile("\\DARAS-NAS\\FORENSOR DTB Dataset processed\\pow_day_125236_07.5.avi"))
print(os.path.isfile("\\\\DARAS-NAS\FORENSOR DTB Dataset processed\pow_day_125236_07.5.avi"))
print(os.path.isfile("\\\\DARAS-NAS\\FORENSOR DTB Dataset processed\\pow_day_125236_07.5.avi"))
print(os.path.isfile("\DARAS-NAS\FORENSOR DTB Dataset processed\pow_day_125236_07.5.avi"))
print(os.path.isfile("/DARAS-NAS/FORENSOR DTB Dataset processed/pow_day_125236_07.5.avi"))
print(os.path.isfile("//DARAS-NAS/FORENSOR DTB Dataset processed/pow_day_125236_07.5.avi"))

##############

print(os.path.isfile(r'V:\FORENSOR DTB Dataset processed\pow_day_125236_07.5.avi'))
print(os.path.isfile(r'V:\\FORENSOR DTB Dataset processed\\pow_day_125236_07.5.avi'))
print(os.path.isfile(r'V:/FORENSOR DTB Dataset processed/pow_day_125236_07.5.avi'))

print(os.path.isfile(r"V:\FORENSOR DTB Dataset processed\pow_day_125236_07.5.avi"))
print(os.path.isfile(r"V:\FORENSOR DTB Dataset processed\pow_day_125236_07.5.avi"))
print(os.path.isfile(r"V:/FORENSOR DTB Dataset processed/pow_day_125236_07.5.avi"))

print(os.path.isfile(r'V:\FORENSOR DTB Dataset processed\pow_day_125236_07.5.avi'))
print(os.path.isfile(r'V:\\FORENSOR DTB Dataset processed\\pow_day_125236_07.5.avi'))
print(os.path.isfile(r'V:/FORENSOR DTB Dataset processed/pow_day_125236_07.5.avi'))

print(os.path.isfile(r'V:\FORENSOR DTB Dataset processed\pow_day_125236_07.5.avi'))
print(os.path.isfile(r'V:\\FORENSOR DTB Dataset processed\\pow_day_125236_07.5.avi'))
print(os.path.isfile(r'V:/FORENSOR DTB Dataset processed/pow_day_125236_07.5.avi'))

此打印:

False
False
False
False
False
False
False
False
False
False
False
False
False
False
False
False
False
False
False
False
False
False
False
False
False
False
False
False
False
False
False
False
False
False
False
False
False
False
False
False

1 个答案:

答案 0 :(得分:0)

也许是这项工作:

public interface IWatch
{
     void Watch();
}

public interface IWatchService<TDataEntity> where TDataEntity : IEntity
{
     INotificationFactory NotificationFactory { get; }
     ObservableCollection<TDataEntity> MatchingEntries { get; set; }
}

public interface IDatabaseWatchService<TDataEntity> : IWatchService<TDataEntity> where TDataEntity : IDatabaseEntity
{
     IDatabaseRepository<IDbManager> DatabaseRepository { get; }
}

public interface IRemoteFilesWatchService<TDataEntity> : IWatchService<TDataEntity> where TDataEntity : IFileEntity
{
     List<string> ExistingRemoteFiles { get; set; }
     List<RemoteLocation> RemoteLocations { get; set; }      
     IWinScpOperations RemoteManager { get; set; }
     IRemoteFilesRepository<IDbManager, TDataEntity> RemoteFilesRepository { get; }
}

public class RemoteFilesWatchService : IRemoteFilesWatchService<IFileEntity>, IWatch
{
     public INotificationFactory NotificationFactory { get; }
     public ObservableCollection<IFileEntity> MatchingEntries { get; set; }
     public List<string> ExistingRemoteFiles { get; set; }
     public List<RemoteLocation> RemoteLocations { get; set; }
     public IWinScpOperations RemoteManager { get; set; }
     public IRemoteFilesRepository<IDbManager, IFileEntity> RemoteFilesRepository { get; }

    public RemoteFilesWatchService(IWinScpOperations remoteOperator,
                IRemoteFilesRepository<IDbManager, IFileEntity> remoteFilesRepository,
                INotificationFactory notificationFactory)
    {
           RemoteManager = remoteOperator;
           RemoteFilesRepository = remoteFilesRepository;  //csv, xml or other repo could be injected
           NotificationFactory = notificationFactory;
    }

    public void Watch()
    {
    }
}

public class DatabaseWatchService : IDatabaseWatchService<DatabaseQuery>, IWatch
{
      public INotificationFactory NotificationFactory { get; }
      public ObservableCollection<DatabaseQuery> MatchingEntries { get; set; }
      public IDatabaseRepository<IDbManager> DatabaseRepository { get; }

      public DatabaseWatchService(IDatabaseRepository<IDbManager> databaseRepository,
            INotificationFactory notificationFactory)
      {
            DatabaseRepository = databaseRepository;
            NotificationFactory = notificationFactory;
      }

      public void Watch()
      {
      }
}

public class CollectionService
{
       private IWatchService<IEntity> _watchService;     

       public CollectionService(IWatchService<IEntity> watchService)
       {
             _watchService = watchService;
       }
}

class Run
{
       void DoIt()
       {          
            IWatchService<IEntity> fileWatcherServiceCsv = new RemoteFilesWatchService(new WinScpOperations(),
                                                                  new RemoteCsvFilesRepository(new DbManager(ConnectionDbType.MySql)),
                                                                  new NotificationFactory());

        var coll1 = new CollectionService(fileWatcherServiceCsv);
        }
}

public interface IEntity
{
}


public interface IFileEntity : IEntity
{
    int Id { get; set; }
    string Name { get; set; }
    bool IsActive { get; set; }
    bool RemoveFromSource { get; set; }
    string DestinationFolder { get; set; }
    RemoteLocation RemoteLocation { get; set; }
}

public interface IDatabaseEntity : IEntity
{
}

public class CsvFile : IFileEntity
{
    public int ColumnHeader { get; set; }
    public int ColumnsCount { get; set; }
    public string Separator { get; set; }
    public int ValuesRowStartposition { get; set; }
    public int ColumnRowPosition { get; set; }
    public int Id { get; set; }
    public string Name { get; set; }
    public bool IsActive { get; set; }
    public bool RemoveFromSource { get; set; }
    public string DestinationFolder { get; set; }
    public RemoteLocation RemoteLocation { get; set; }
}

public class XmlFile : IFileEntity
{
    public int Id { get; set; }
    public string Name { get; set; }
    public bool IsActive { get; set; }
    public bool RemoveFromSource { get; set; }
    public string DestinationFolder { get; set; }
    public RemoteLocation RemoteLocation { get; set; }
    public string SubNode { get; set; }
    public string MainNode { get; set; }
}

只需调用它,文件将是文件的路径,文件名为它自己。